Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreditmemoRepository.php
Go to the documentation of this file.
1 <?php
8 
15 use Magento\Sales\Api\Data\CreditmemoSearchResultInterfaceFactory as SearchResultFactory;
17 
23 {
27  protected $metadata;
28 
32  protected $searchResultFactory = null;
33 
39  protected $registry = [];
40 
44  private $collectionProcessor;
45 
52  public function __construct(
54  SearchResultFactory $searchResultFactory,
55  CollectionProcessorInterface $collectionProcessor = null
56  ) {
57  $this->metadata = $metadata;
58  $this->searchResultFactory = $searchResultFactory;
59  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
60  }
61 
70  public function get($id)
71  {
72  if (!$id) {
73  throw new InputException(__('An ID is needed. Set the ID and try again.'));
74  }
75  if (!isset($this->registry[$id])) {
77  $entity = $this->metadata->getNewInstance()->load($id);
78  if (!$entity->getEntityId()) {
79  throw new NoSuchEntityException(
80  __("The entity that was requested doesn't exist. Verify the entity and try again.")
81  );
82  }
83  $this->registry[$id] = $entity;
84  }
85  return $this->registry[$id];
86  }
87 
93  public function create()
94  {
95  return $this->metadata->getNewInstance();
96  }
97 
104  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
105  {
107  $searchResult = $this->searchResultFactory->create();
108  $this->collectionProcessor->process($searchCriteria, $searchResult);
109  $searchResult->setSearchCriteria($searchCriteria);
110  return $searchResult;
111  }
112 
121  {
122  try {
123  $this->metadata->getMapper()->delete($entity);
124  unset($this->registry[$entity->getEntityId()]);
125  } catch (\Exception $e) {
126  throw new CouldNotDeleteException(__("The credit memo couldn't be deleted."), $e);
127  }
128  return true;
129  }
130 
138  public function save(\Magento\Sales\Api\Data\CreditmemoInterface $entity)
139  {
140  try {
141  $this->metadata->getMapper()->save($entity);
142  $this->registry[$entity->getEntityId()] = $entity;
143  } catch (LocalizedException $e) {
144  throw new CouldNotSaveException(__($e->getMessage()), $e);
145  } catch (\Exception $e) {
146  throw new CouldNotSaveException(__("The credit memo couldn't be saved."), $e);
147  }
148  return $this->registry[$entity->getEntityId()];
149  }
150 
157  private function getCollectionProcessor()
158  {
159  if (!$this->collectionProcessor) {
160  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
161  \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
162  );
163  }
164  return $this->collectionProcessor;
165  }
166 }
save(\Magento\Sales\Api\Data\CreditmemoInterface $entity)
__construct(Metadata $metadata, SearchResultFactory $searchResultFactory, CollectionProcessorInterface $collectionProcessor=null)
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$searchCriteria
$entity
Definition: element.phtml:22