123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- /**
- * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
- */
- namespace Temando\Shipping\Model\ResourceModel\Shipment;
- use Magento\Framework\Api\FilterBuilder;
- use Magento\Framework\Api\Search\SearchCriteriaBuilderFactory;
- use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
- use Magento\Framework\Api\SearchCriteriaInterface;
- use Magento\Framework\Api\SortOrder;
- use Magento\Framework\Exception\CouldNotSaveException;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Sales\Api\ShipmentTrackRepositoryInterface;
- use Temando\Shipping\Api\Data\Shipment\ShipmentReferenceInterface;
- use Temando\Shipping\Api\Data\Shipment\ShipmentReferenceInterfaceFactory;
- use Temando\Shipping\Model\ResourceModel\Repository\ShipmentReferenceRepositoryInterface;
- use Temando\Shipping\Model\ResourceModel\Shipment\ShipmentReference as ShipmentReferenceResource;
- /**
- * Temando Shipment Repository
- *
- * @package Temando\Shipping\Model
- * @author Christoph Aßmann <christoph.assmann@netresearch.de>
- * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
- * @link https://www.temando.com/
- */
- class ShipmentReferenceRepository implements ShipmentReferenceRepositoryInterface
- {
- /**
- * @var ShipmentReferenceResource
- */
- private $resource;
- /**
- * @var ShipmentReferenceInterfaceFactory
- */
- private $shipmentReferenceFactory;
- /**
- * @var ShipmentReferenceCollectionFactory
- */
- private $shipmentReferenceCollectionFactory;
- /**
- * @var CollectionProcessorInterface
- */
- private $collectionProcessor;
- /**
- * @var SearchCriteriaBuilderFactory
- */
- private $searchCriteriaBuilderFactory;
- /**
- * @var FilterBuilder
- */
- private $filterBuilder;
- /**
- * @var ShipmentTrackRepositoryInterface
- */
- private $shipmentTrackRepository;
- /**
- * ShipmentReferenceRepository constructor.
- * @param ShipmentReference $resource
- * @param ShipmentReferenceInterfaceFactory $shipmentReferenceFactory
- * @param ShipmentReferenceCollectionFactory $shipmentReferenceCollectionFactory
- * @param CollectionProcessorInterface $collectionProcessor
- * @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
- * @param FilterBuilder $filterBuilder
- * @param ShipmentTrackRepositoryInterface $shipmentTrackRepository
- */
- public function __construct(
- ShipmentReferenceResource $resource,
- ShipmentReferenceInterfaceFactory $shipmentReferenceFactory,
- ShipmentReferenceCollectionFactory $shipmentReferenceCollectionFactory,
- CollectionProcessorInterface $collectionProcessor,
- SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
- FilterBuilder $filterBuilder,
- ShipmentTrackRepositoryInterface $shipmentTrackRepository
- ) {
- $this->resource = $resource;
- $this->shipmentReferenceFactory = $shipmentReferenceFactory;
- $this->shipmentReferenceCollectionFactory = $shipmentReferenceCollectionFactory;
- $this->collectionProcessor = $collectionProcessor;
- $this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
- $this->filterBuilder = $filterBuilder;
- $this->shipmentTrackRepository = $shipmentTrackRepository;
- }
- /**
- * Load local track info.
- *
- * @param string $carrierCode
- * @param string $trackingNumber
- * @return \Magento\Sales\Api\Data\ShipmentTrackInterface
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getShipmentTrack($carrierCode, $trackingNumber)
- {
- $numberFilter = $this->filterBuilder
- ->setField('track_number')
- ->setValue($trackingNumber)
- ->setConditionType('eq')
- ->create();
- $carrierFilter = $this->filterBuilder
- ->setField('carrier_code')
- ->setValue($carrierCode)
- ->setConditionType('eq')
- ->create();
- // builder does not get reset properly on `create()`, instantiate a fresh one…
- $searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
- $searchCriteria = $searchCriteriaBuilder
- ->addFilter($numberFilter)
- ->addFilter($carrierFilter)
- ->addSortOrder('entity_id', SortOrder::SORT_DESC)
- ->setPageSize(1)
- ->create();
- /** @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection $shipmentTracksCollection */
- $shipmentTracksCollection = $this->shipmentTrackRepository->getList($searchCriteria);
- /** @var \Magento\Sales\Model\Order\Shipment\Track $shipmentTrack */
- $shipmentTrack = $shipmentTracksCollection->fetchItem();
- if (!$shipmentTrack) {
- throw NoSuchEntityException::singleField('track_number', $trackingNumber);
- }
- return $shipmentTrack;
- }
- /**
- * Save local reference to external shipment entity.
- *
- * @param ShipmentReferenceInterface $shipment
- * @return ShipmentReferenceInterface
- * @throws CouldNotSaveException
- */
- public function save(ShipmentReferenceInterface $shipment)
- {
- try {
- /** @var \Temando\Shipping\Model\Shipment\ShipmentReference $shipment */
- $this->resource->save($shipment);
- } catch (\Exception $exception) {
- throw new CouldNotSaveException(__('Unable to save shipment reference.'), $exception);
- }
- return $shipment;
- }
- /**
- * Load local reference to external shipment entity.
- *
- * @param int $entityId
- * @return ShipmentReferenceInterface
- * @throws NoSuchEntityException
- */
- public function getById($entityId)
- {
- /** @var \Temando\Shipping\Model\Shipment\ShipmentReference $shipment */
- $shipment = $this->shipmentReferenceFactory->create();
- $this->resource->load($shipment, $entityId);
- if (!$shipment->getId()) {
- throw new NoSuchEntityException(__('Shipment with id "%1" does not exist.', $entityId));
- }
- return $shipment;
- }
- /**
- * Load local reference to external shipment entity by Magento shipment ID.
- *
- * @param int $shipmentId
- * @return ShipmentReferenceInterface
- * @throws NoSuchEntityException
- */
- public function getByShipmentId($shipmentId)
- {
- $entityId = $this->resource->getIdByShipmentId($shipmentId);
- return $this->getById($entityId);
- }
- /**
- * Load local reference to external shipment entity by Temando shipment ID.
- *
- * @param string $extShipmentId
- *
- * @return ShipmentReferenceInterface
- * @throws NoSuchEntityException
- */
- public function getByExtShipmentId($extShipmentId)
- {
- $entityId = $this->resource->getIdByExtShipmentId($extShipmentId);
- return $this->getById($entityId);
- }
- /**
- * Load local reference to external shipment entity by Temando return shipment ID.
- *
- * @param string $extShipmentId
- *
- * @return ShipmentReferenceInterface
- * @throws NoSuchEntityException
- */
- public function getByExtReturnShipmentId($extShipmentId)
- {
- $entityId = $this->resource->getIdByExtReturnShipmentId($extShipmentId);
- return $this->getById($entityId);
- }
- /**
- * List shipment references that match specified search criteria.
- *
- * @param SearchCriteriaInterface $searchCriteria
- * @return ShipmentReferenceCollection
- */
- public function getList(SearchCriteriaInterface $searchCriteria)
- {
- $collection = $this->shipmentReferenceCollectionFactory->create();
- $this->collectionProcessor->process($searchCriteria, $collection);
- return $collection;
- }
- }
|