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
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
*/
namespace Temando\Shipping\Model\ResourceModel\Delivery;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Temando\Shipping\Api\Data\Delivery\CollectionPointSearchResultInterface;
use Temando\Shipping\Api\Data\Delivery\CollectionPointSearchResultInterfaceFactory;
use Temando\Shipping\Api\Data\Delivery\QuoteCollectionPointInterface;
use Temando\Shipping\Api\Data\Delivery\QuoteCollectionPointInterfaceFactory;
use Temando\Shipping\Model\Delivery\QuoteCollectionPoint;
use Temando\Shipping\Model\ResourceModel\Delivery\QuoteCollectionPoint as CollectionPointResource;
use Temando\Shipping\Model\ResourceModel\Repository\QuoteCollectionPointRepositoryInterface;
/**
* Temando Quote Collection Point Repository
*
* @package Temando\Shipping\Model
* @author Christoph Aßmann <christoph.assmann@netresearch.de>
* @author Sebastian Ertner <sebastian.ertner@netresearch.de>
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://www.temando.com/
*/
class QuoteCollectionPointRepository implements QuoteCollectionPointRepositoryInterface
{
/**
* @var CollectionPointResource
*/
private $resource;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var QuoteCollectionPointInterfaceFactory
*/
private $collectionPointFactory;
/**
* @var CollectionPointSearchResultInterfaceFactory
*/
private $collectionPointSearchResultFactory;
/**
* @var CollectionProcessorInterface
*/
private $collectionProcessor;
/**
* CollectionPointRepository constructor.
* @param CollectionPointResource $resource
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param QuoteCollectionPointInterfaceFactory $collectionPointFactory
* @param CollectionPointSearchResultInterfaceFactory $collectionPointSearchResultFactory
* @param CollectionProcessorInterface $collectionProcessor
*/
public function __construct(
CollectionPointResource $resource,
SearchCriteriaBuilder $searchCriteriaBuilder,
QuoteCollectionPointInterfaceFactory $collectionPointFactory,
CollectionPointSearchResultInterfaceFactory $collectionPointSearchResultFactory,
CollectionProcessorInterface $collectionProcessor
) {
$this->resource = $resource;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->collectionPointFactory = $collectionPointFactory;
$this->collectionPointSearchResultFactory = $collectionPointSearchResultFactory;
$this->collectionProcessor = $collectionProcessor;
}
/**
* Load collection point by entity id.
*
* @param int $entityId
* @return QuoteCollectionPointInterface
* @throws NoSuchEntityException
*/
public function get($entityId)
{
/** @var QuoteCollectionPoint $collectionPoint */
$collectionPoint = $this->collectionPointFactory->create();
try {
$this->resource->load($collectionPoint, $entityId);
} catch (\Exception $exception) {
throw new NoSuchEntityException(__('Collection point with id "%1" does not exist.', $entityId));
}
return $collectionPoint;
}
/**
* Load selected collection point for given shipping address ID.
*
* Beware: AbstractDb::fetchItem will NOT decode serialized fields.
*
* @param int $addressId
* @return QuoteCollectionPointInterface
* @throws NoSuchEntityException
*/
public function getSelected($addressId)
{
$this->searchCriteriaBuilder->addFilter(
QuoteCollectionPointInterface::RECIPIENT_ADDRESS_ID,
$addressId
);
$this->searchCriteriaBuilder->addFilter(
QuoteCollectionPointInterface::SELECTED,
true
);
$this->searchCriteriaBuilder->setPageSize(1);
$this->searchCriteriaBuilder->setCurrentPage(1);
$criteria = $this->searchCriteriaBuilder->create();
/** @var CollectionPointSearchResult $searchResult */
$searchResult = $this->getList($criteria);
/** @var QuoteCollectionPointInterface $collectionPoint */
$collectionPoint = $searchResult->fetchItem();
if (!$collectionPoint) {
$msg = __('Selected collection point for address id "%1" does not exist.', $addressId);
throw new NoSuchEntityException($msg);
}
return $collectionPoint;
}
/**
* Save collection point.
*
* @param QuoteCollectionPointInterface $collectionPoint
* @return QuoteCollectionPointInterface
* @throws CouldNotSaveException
*/
public function save(QuoteCollectionPointInterface $collectionPoint)
{
try {
/** @var QuoteCollectionPoint $collectionPoint */
$this->resource->save($collectionPoint);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__('Unable to save collection point.'), $exception);
}
return $collectionPoint;
}
/**
* Delete collection point.
*
* @param QuoteCollectionPointInterface $collectionPoint
* @return bool
* @throws CouldNotDeleteException
*/
public function delete(QuoteCollectionPointInterface $collectionPoint)
{
try {
/** @var QuoteCollectionPoint $collectionPoint */
$this->resource->delete($collectionPoint);
} catch (\Exception $exception) {
throw new CouldNotDeleteException(__('Unable to delete collection point.'), $exception);
}
return true;
}
/**
* Load collection points.
*
* @param SearchCriteriaInterface $criteria
* @return CollectionPointSearchResultInterface|CollectionPointSearchResult
*/
public function getList(SearchCriteriaInterface $criteria)
{
/** @var CollectionPointSearchResult $searchResult */
$searchResult = $this->collectionPointSearchResultFactory->create();
$searchResult->addExpressionFieldToSelect(
'sort_distance',
'COALESCE({{sd}}, POW(2, 32))',
['sd' => QuoteCollectionPointInterface::DISTANCE]
);
$this->collectionProcessor->process($criteria, $searchResult);
$searchResult->setSearchCriteria($criteria);
return $searchResult;
}
}