123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Model\Order;
- use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
- use Magento\Sales\Api\InvoiceRepositoryInterface;
- use Magento\Sales\Model\ResourceModel\Metadata;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Sales\Api\Data\InvoiceSearchResultInterfaceFactory as SearchResultFactory;
- /**
- * Class InvoiceRepository
- */
- class InvoiceRepository implements InvoiceRepositoryInterface
- {
- /**
- * \Magento\Sales\Api\Data\InvoiceInterface[]
- *
- * @var array
- */
- protected $registry = [];
- /**
- * @var Metadata
- */
- protected $metadata;
- /**
- * @var SearchResultFactory
- */
- protected $searchResultFactory;
- /**
- * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
- */
- private $collectionProcessor;
- /**
- * InvoiceRepository constructor.
- * @param Metadata $invoiceMetadata
- * @param SearchResultFactory $searchResultFactory
- * @param CollectionProcessorInterface|null $collectionProcessor
- */
- public function __construct(
- Metadata $invoiceMetadata,
- SearchResultFactory $searchResultFactory,
- CollectionProcessorInterface $collectionProcessor = null
- ) {
- $this->metadata = $invoiceMetadata;
- $this->searchResultFactory = $searchResultFactory;
- $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
- }
- /**
- * Load entity
- *
- * @param int $id
- * @return mixed
- * @throws NoSuchEntityException
- * @throws \Magento\Framework\Exception\InputException
- */
- public function get($id)
- {
- if (!$id) {
- throw new \Magento\Framework\Exception\InputException(__('An ID is needed. Set the ID and try again.'));
- }
- if (!isset($this->registry[$id])) {
- /** @var \Magento\Sales\Api\Data\InvoiceInterface $entity */
- $entity = $this->metadata->getNewInstance()->load($id);
- if (!$entity->getEntityId()) {
- throw new NoSuchEntityException(
- __("The entity that was requested doesn't exist. Verify the entity and try again.")
- );
- }
- $this->registry[$id] = $entity;
- }
- return $this->registry[$id];
- }
- /**
- * @return \Magento\Sales\Api\Data\InvoiceInterface
- */
- public function create()
- {
- return $this->metadata->getNewInstance();
- }
- /**
- * Find entities by criteria
- *
- * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
- * @return \Magento\Sales\Api\Data\InvoiceInterface[]
- */
- public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
- {
- /** @var \Magento\Sales\Model\ResourceModel\Order\Invoice\Collection $collection */
- $collection = $this->searchResultFactory->create();
- $this->collectionProcessor->process($searchCriteria, $collection);
- $collection->setSearchCriteria($searchCriteria);
- return $collection;
- }
- /**
- * Register entity to delete
- *
- * @param \Magento\Sales\Api\Data\InvoiceInterface $entity
- * @return bool
- */
- public function delete(\Magento\Sales\Api\Data\InvoiceInterface $entity)
- {
- $this->metadata->getMapper()->delete($entity);
- unset($this->registry[$entity->getEntityId()]);
- return true;
- }
- /**
- * Delete entity by Id
- *
- * @param int $id
- * @return bool
- */
- public function deleteById($id)
- {
- $entity = $this->get($id);
- return $this->delete($entity);
- }
- /**
- * Perform persist operations for one entity
- *
- * @param \Magento\Sales\Api\Data\InvoiceInterface $entity
- * @return \Magento\Sales\Api\Data\InvoiceInterface
- */
- public function save(\Magento\Sales\Api\Data\InvoiceInterface $entity)
- {
- $this->metadata->getMapper()->save($entity);
- $this->registry[$entity->getEntityId()] = $entity;
- return $this->registry[$entity->getEntityId()];
- }
- /**
- * Retrieve collection processor
- *
- * @deprecated 101.0.0
- * @return CollectionProcessorInterface
- */
- private function getCollectionProcessor()
- {
- if (!$this->collectionProcessor) {
- $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
- \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
- );
- }
- return $this->collectionProcessor;
- }
- }
|