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
<?php
/**
* Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
*/
namespace Temando\Shipping\Model\ResourceModel\Batch;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Temando\Shipping\Model\BatchInterface;
use Temando\Shipping\Model\ResourceModel\Repository\BatchRepositoryInterface;
use Temando\Shipping\Rest\Adapter\BatchApiInterface;
use Temando\Shipping\Rest\EntityMapper\BatchResponseMapper;
use Temando\Shipping\Rest\Exception\AdapterException;
use Temando\Shipping\Rest\Request\ItemRequestInterfaceFactory;
/**
* Temando Batch Repository
*
* @package Temando\Shipping\Model
* @author Rhodri Davies <rhodri.davies@temando.com>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link http://www.temando.com/
*/
class BatchRepository implements BatchRepositoryInterface
{
/**
* @var BatchApiInterface
*/
private $apiAdapter;
/**
* @var ItemRequestInterfaceFactory
*/
private $batchRequestFactory;
/**
* @var BatchResponseMapper
*/
private $batchMapper;
/**
* BatchRepository constructor.
* @param BatchApiInterface $apiAdapter
* @param ItemRequestInterfaceFactory $batchRequestFactory
* @param BatchResponseMapper $batchMapper
*/
public function __construct(
BatchApiInterface $apiAdapter,
ItemRequestInterfaceFactory $batchRequestFactory,
BatchResponseMapper $batchMapper
) {
$this->apiAdapter = $apiAdapter;
$this->batchRequestFactory = $batchRequestFactory;
$this->batchMapper = $batchMapper;
}
/**
* @param string $batchId
* @return BatchInterface
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function getById($batchId)
{
if (!$batchId) {
throw new LocalizedException(__('An error occurred while loading data.'));
}
try {
$request = $this->batchRequestFactory->create(['entityId' => $batchId]);
$apiBatch = $this->apiAdapter->getBatch($request);
$batch = $this->batchMapper->map($apiBatch);
} catch (AdapterException $e) {
if ($e->getCode() === 404) {
throw NoSuchEntityException::singleField('batchId', $batchId);
}
throw new LocalizedException(__('An error occurred while loading data.'), $e);
}
return $batch;
}
}