Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShipmentRepository.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Sales\Api\Data\ShipmentSearchResultInterfaceFactory as SearchResultFactory;
16 
23 {
27  protected $metadata;
28 
32  protected $searchResultFactory = null;
33 
37  protected $registry = [];
38 
42  private $collectionProcessor;
43 
49  public function __construct(
51  SearchResultFactory $searchResultFactory,
52  CollectionProcessorInterface $collectionProcessor = null
53  ) {
54  $this->metadata = $metadata;
55  $this->searchResultFactory = $searchResultFactory;
56  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
57  }
58 
67  public function get($id)
68  {
69  if (!$id) {
70  throw new InputException(__('An ID is needed. Set the ID and try again.'));
71  }
72 
73  if (!isset($this->registry[$id])) {
75  $entity = $this->metadata->getNewInstance()->load($id);
76  if (!$entity->getEntityId()) {
77  throw new NoSuchEntityException(
78  __("The entity that was requested doesn't exist. Verify the entity and try again.")
79  );
80  }
81 
82  $this->registry[$id] = $entity;
83  }
84 
85  return $this->registry[$id];
86  }
87 
94  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
95  {
97  $searchResult = $this->searchResultFactory->create();
98  $this->collectionProcessor->process($searchCriteria, $searchResult);
99  $searchResult->setSearchCriteria($searchCriteria);
100 
101  return $searchResult;
102  }
103 
112  {
113  try {
114  $this->metadata->getMapper()->delete($entity);
115 
116  unset($this->registry[$entity->getEntityId()]);
117  } catch (\Exception $e) {
118  throw new CouldNotDeleteException(__("The shipment couldn't be deleted."), $e);
119  }
120 
121  return true;
122  }
123 
130  public function deleteById($id)
131  {
132  $entity = $this->get($id);
133 
134  return $this->delete($entity);
135  }
136 
144  public function save(\Magento\Sales\Api\Data\ShipmentInterface $entity)
145  {
146  try {
147  $this->metadata->getMapper()->save($entity);
148  $this->registry[$entity->getEntityId()] = $entity;
149  } catch (\Exception $e) {
150  throw new CouldNotSaveException(__("The shipment couldn't be saved."), $e);
151  }
152 
153  return $this->registry[$entity->getEntityId()];
154  }
155 
161  public function create()
162  {
163  return $this->metadata->getNewInstance();
164  }
165 
172  private function getCollectionProcessor()
173  {
174  if (!$this->collectionProcessor) {
175  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
176  \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
177  );
178  }
179  return $this->collectionProcessor;
180  }
181 }
save(\Magento\Sales\Api\Data\ShipmentInterface $entity)
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$searchCriteria
$entity
Definition: element.phtml:22
__construct(Metadata $metadata, SearchResultFactory $searchResultFactory, CollectionProcessorInterface $collectionProcessor=null)