CarrierRepository.php 5.8 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */
namespace Temando\Shipping\Model\ResourceModel\Carrier;

use Magento\Framework\Exception\CouldNotDeleteException;
use Psr\Log\LoggerInterface;
use Temando\Shipping\Model\CarrierInterface;
use Temando\Shipping\Model\ResourceModel\Repository\CarrierRepositoryInterface;
use Temando\Shipping\Rest\Adapter\CarrierApiInterface;
use Temando\Shipping\Rest\EntityMapper\CarrierResponseMapper;
use Temando\Shipping\Rest\Exception\AdapterException;
use Temando\Shipping\Rest\Request\ItemRequestInterfaceFactory;
use Temando\Shipping\Rest\Request\ListRequestInterfaceFactory;
use Temando\Shipping\Rest\Response\DataObject\CarrierConfiguration;
use Temando\Shipping\Rest\Response\DataObject\CarrierIntegration;
use Temando\Shipping\Webservice\Filter\CollectionFilterFactory;
use Temando\Shipping\Webservice\Pagination\PaginationFactory;

/**
 * Temando Carrier Repository
 *
 * @package Temando\Shipping\Model
 * @author  Sebastian Ertner <sebastian.ertner@netresearch.de>
 * @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 CarrierRepository implements CarrierRepositoryInterface
{
    /**
     * @var CarrierApiInterface
     */
    private $apiAdapter;

    /**
     * @var CollectionFilterFactory
     */
    private $filterFactory;

    /**
     * @var PaginationFactory
     */
    private $paginationFactory;

    /**
     * @var ListRequestInterfaceFactory
     */
    private $requestFactory;

    /**
     * @var ItemRequestInterfaceFactory
     */
    private $itemRequestFactory;

    /**
     * @var CarrierResponseMapper
     */
    private $carrierMapper;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * CarrierRepository constructor.
     *
     * @param CarrierApiInterface $apiAdapter
     * @param CollectionFilterFactory $filterFactory
     * @param PaginationFactory $paginationFactory
     * @param ListRequestInterfaceFactory $requestFactory
     * @param ItemRequestInterfaceFactory $itemRequestFactory
     * @param CarrierResponseMapper $carrierMapper
     * @param LoggerInterface $logger
     */
    public function __construct(
        CarrierApiInterface $apiAdapter,
        CollectionFilterFactory $filterFactory,
        PaginationFactory $paginationFactory,
        ListRequestInterfaceFactory $requestFactory,
        ItemRequestInterfaceFactory $itemRequestFactory,
        CarrierResponseMapper $carrierMapper,
        LoggerInterface $logger
    ) {
        $this->apiAdapter = $apiAdapter;
        $this->filterFactory = $filterFactory;
        $this->paginationFactory = $paginationFactory;
        $this->requestFactory = $requestFactory;
        $this->itemRequestFactory = $itemRequestFactory;
        $this->carrierMapper = $carrierMapper;
        $this->logger = $logger;
    }

    /**
     * @param null $offset
     * @param null $limit
     * @return CarrierIntegration[]
     * @throws AdapterException
     */
    private function getIntegrations($offset = null, $limit = null)
    {
        $pagination = $this->paginationFactory->create([
            'offset' => $offset,
            'limit' => $limit,
        ]);
        $filter = $this->filterFactory->create([
            'filters' => ['registered' => 'true'],
        ]);

        $request = $this->requestFactory->create([
            'pagination' => $pagination,
            'filter' => $filter,
        ]);

        return $this->apiAdapter->getCarrierIntegrations($request);
    }

    /**
     * @param null $offset
     * @param null $limit
     * @return CarrierConfiguration[]
     * @throws AdapterException
     */
    private function getConfigurations($offset = null, $limit = null)
    {
        $pagination = $this->paginationFactory->create([
            'offset' => $offset,
            'limit' => $limit,
        ]);

        $request = $this->requestFactory->create([
            'pagination' => $pagination,
        ]);

        return $this->apiAdapter->getCarrierConfigurations($request);
    }

    /**
     * @param int|null $offset
     * @param int|null $limit
     * @return CarrierInterface[]
     */
    public function getList($offset = null, $limit = null)
    {
        try {
            $apiIntegrations = $this->getIntegrations($offset, $limit);
            $integrations = [];
            foreach ($apiIntegrations as $apiCarrierIntegration) {
                $integrations[$apiCarrierIntegration->getId()] = $apiCarrierIntegration;
            }

            $apiConfigurations = $this->getConfigurations($offset, $limit);
            $carriers = array_map(function (CarrierConfiguration $apiConfiguration) use ($integrations) {
                $integrationId = $apiConfiguration->getAttributes()->getIntegrationId();
                $integration = isset($integrations[$integrationId]) ? $integrations[$integrationId] : null;
                return $this->carrierMapper->map($apiConfiguration, $integration);
            }, $apiConfigurations);
        } catch (AdapterException $e) {
            $this->logger->critical($e->getMessage(), ['exception' => $e]);
            $carriers = [];
        }

        return $carriers;
    }

    /**
     * @param string $carrierConfigurationId
     *
     * @return void
     * @throws CouldNotDeleteException
     */
    public function delete($carrierConfigurationId)
    {
        try {
            $request = $this->itemRequestFactory->create(['entityId' => $carrierConfigurationId]);
            $this->apiAdapter->deleteCarrierConfiguration($request);
        } catch (AdapterException $e) {
            $this->logger->critical($e->getMessage(), ['exception' => $e]);
            throw new CouldNotDeleteException(__('Unable to delete carrier.'), $e);
        }
    }
}