Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InvoiceRepository.php
Go to the documentation of this file.
1 <?php
8 
13 use Magento\Sales\Api\Data\InvoiceSearchResultInterfaceFactory as SearchResultFactory;
14 
19 {
25  protected $registry = [];
26 
30  protected $metadata;
31 
36 
40  private $collectionProcessor;
41 
48  public function __construct(
49  Metadata $invoiceMetadata,
50  SearchResultFactory $searchResultFactory,
51  CollectionProcessorInterface $collectionProcessor = null
52  ) {
53  $this->metadata = $invoiceMetadata;
54  $this->searchResultFactory = $searchResultFactory;
55  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
56  }
57 
66  public function get($id)
67  {
68  if (!$id) {
69  throw new \Magento\Framework\Exception\InputException(__('An ID is needed. Set the ID and try again.'));
70  }
71  if (!isset($this->registry[$id])) {
73  $entity = $this->metadata->getNewInstance()->load($id);
74  if (!$entity->getEntityId()) {
75  throw new NoSuchEntityException(
76  __("The entity that was requested doesn't exist. Verify the entity and try again.")
77  );
78  }
79  $this->registry[$id] = $entity;
80  }
81  return $this->registry[$id];
82  }
83 
87  public function create()
88  {
89  return $this->metadata->getNewInstance();
90  }
91 
98  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
99  {
101  $collection = $this->searchResultFactory->create();
102  $this->collectionProcessor->process($searchCriteria, $collection);
103  $collection->setSearchCriteria($searchCriteria);
104  return $collection;
105  }
106 
114  {
115  $this->metadata->getMapper()->delete($entity);
116  unset($this->registry[$entity->getEntityId()]);
117  return true;
118  }
119 
126  public function deleteById($id)
127  {
128  $entity = $this->get($id);
129  return $this->delete($entity);
130  }
131 
138  public function save(\Magento\Sales\Api\Data\InvoiceInterface $entity)
139  {
140  $this->metadata->getMapper()->save($entity);
141  $this->registry[$entity->getEntityId()] = $entity;
142  return $this->registry[$entity->getEntityId()];
143  }
144 
151  private function getCollectionProcessor()
152  {
153  if (!$this->collectionProcessor) {
154  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
155  \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
156  );
157  }
158  return $this->collectionProcessor;
159  }
160 }
$id
Definition: fieldset.phtml:14
save(\Magento\Sales\Api\Data\InvoiceInterface $entity)
__()
Definition: __.php:13
$searchCriteria
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
__construct(Metadata $invoiceMetadata, SearchResultFactory $searchResultFactory, CollectionProcessorInterface $collectionProcessor=null)
$entity
Definition: element.phtml:22