Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Repository.php
Go to the documentation of this file.
1 <?php
8 
16 use Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory as SearchResultFactory;
19 use Magento\Sales\Model\EntityStorageFactory;
22 
28 {
34  private $searchResultFactory = null;
35 
39  private $filterBuilder;
40 
44  private $searchCriteriaBuilder;
45 
49  private $metaData;
50 
54  private $sortOrderBuilder;
55 
59  private $entityStorage;
60 
64  private $collectionProcessor;
65 
76  public function __construct(
77  SearchResultFactory $searchResultFactory,
78  FilterBuilder $filterBuilder,
79  SearchCriteriaBuilder $searchCriteriaBuilder,
80  SortOrderBuilder $sortOrderBuilder,
81  Metadata $metaData,
82  EntityStorageFactory $entityStorageFactory,
83  CollectionProcessorInterface $collectionProcessor = null
84  ) {
85  $this->searchResultFactory = $searchResultFactory;
86  $this->filterBuilder = $filterBuilder;
87  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
88  $this->sortOrderBuilder = $sortOrderBuilder;
89  $this->metaData = $metaData;
90  $this->entityStorage = $entityStorageFactory->create();
91  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
92  }
93 
97  public function get($id)
98  {
99  if (!$id) {
100  throw new \Magento\Framework\Exception\InputException(__('An ID is needed. Set the ID and try again.'));
101  }
102  if (!$this->entityStorage->has($id)) {
103  $entity = $this->metaData->getNewInstance()->load($id);
104  if (!$entity->getTransactionId()) {
105  throw new NoSuchEntityException(
106  __("The entity that was requested doesn't exist. Verify the entity and try again.")
107  );
108  }
109  $this->entityStorage->add($entity);
110  }
111  return $this->entityStorage->get($id);
112  }
113 
120  public function getByTransactionType($transactionType, $paymentId)
121  {
122  $identityFieldsForCache = [$transactionType, $paymentId];
123  $cacheStorage = 'txn_type';
124  $entity = $this->entityStorage->getByIdentifyingFields($identityFieldsForCache, $cacheStorage);
125  if (!$entity) {
126  $typeFilter = $this->filterBuilder
128  ->setValue($transactionType)
129  ->create();
130  $idFilter = $this->filterBuilder
132  ->setValue($paymentId)
133  ->create();
134  $transactionIdSort = $this->sortOrderBuilder
135  ->setField('transaction_id')
136  ->setDirection(Collection::SORT_ORDER_DESC)
137  ->create();
138  $createdAtSort = $this->sortOrderBuilder
139  ->setField('created_at')
140  ->setDirection(Collection::SORT_ORDER_DESC)
141  ->create();
142  $entity = current(
143  $this->getList(
144  $this->searchCriteriaBuilder
145  ->addFilters([$typeFilter])
146  ->addFilters([$idFilter])
147  ->addSortOrder($transactionIdSort)
148  ->addSortOrder($createdAtSort)
149  ->create()
150  )->getItems()
151  );
152  if ($entity) {
153  $this->entityStorage->addByIdentifyingFields($entity, $identityFieldsForCache, $cacheStorage);
154  }
155  }
156 
157  return $entity;
158  }
159 
167  public function getByTransactionId($transactionId, $paymentId, $orderId)
168  {
169  $identityFieldsForCache = [$transactionId, $paymentId, $orderId];
170  $cacheStorage = 'txn_id';
171  $entity = $this->entityStorage->getByIdentifyingFields($identityFieldsForCache, $cacheStorage);
172  if (!$entity) {
173  $entity = $this->metaData->getNewInstance();
174  $this->metaData->getMapper()->loadObjectByTxnId(
175  $entity,
176  $orderId,
177  $paymentId,
178  $transactionId
179  );
180  if ($entity && $entity->getId()) {
181  $this->entityStorage->addByIdentifyingFields($entity, $identityFieldsForCache, $cacheStorage);
182  return $entity;
183  }
184  return false;
185  }
186  return $entity;
187  }
188 
192  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
193  {
195  $collection = $this->searchResultFactory->create();
196  $collection->setSearchCriteria($searchCriteria);
197  $this->collectionProcessor->process($searchCriteria, $collection);
198  $collection->addPaymentInformation(['method']);
199  $collection->addOrderInformation(['increment_id']);
200  return $collection;
201  }
202 
207  {
208  $this->metaData->getMapper()->delete($entity);
209  $this->entityStorage->remove($entity->getTransactionId());
210  return true;
211  }
212 
216  public function save(\Magento\Sales\Api\Data\TransactionInterface $entity)
217  {
218  $this->metaData->getMapper()->save($entity);
219  $this->entityStorage->add($entity);
220  return $this->entityStorage->get($entity->getTransactionId());
221  }
222 
228  public function create()
229  {
230  return $this->metaData->getNewInstance();
231  }
232 
239  private function getCollectionProcessor()
240  {
241  if (!$this->collectionProcessor) {
242  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
243  \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
244  );
245  }
246  return $this->collectionProcessor;
247  }
248 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$searchCriteria
save(\Magento\Sales\Api\Data\TransactionInterface $entity)
Definition: Repository.php:216
$entity
Definition: element.phtml:22
$searchCriteriaBuilder
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
getByTransactionId($transactionId, $paymentId, $orderId)
Definition: Repository.php:167
__construct(SearchResultFactory $searchResultFactory, FilterBuilder $filterBuilder, SearchCriteriaBuilder $searchCriteriaBuilder, SortOrderBuilder $sortOrderBuilder, Metadata $metaData, EntityStorageFactory $entityStorageFactory, CollectionProcessorInterface $collectionProcessor=null)
Definition: Repository.php:76