-
Notifications
You must be signed in to change notification settings - Fork 248
/
InStorePickup.php
189 lines (162 loc) · 5.15 KB
/
InStorePickup.php
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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventoryInStorePickupShippingApi\Model\Carrier;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\InventoryInStorePickupShippingApi\Model\Carrier\Command\GetShippingPriceInterface;
use Magento\InventoryInStorePickupShippingApi\Model\Carrier\Validation\RequestValidatorInterface;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Quote\Model\Quote\Address\RateResult\Error;
use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
use Magento\Quote\Model\Quote\Address\RateResult\Method;
use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;
use Magento\Shipping\Model\Rate\ResultFactory;
use Magento\Store\Api\Data\StoreInterface;
use Psr\Log\LoggerInterface;
/**
* In-Store Pickup Delivery Method Model.
*
* @api
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class InStorePickup extends AbstractCarrier implements CarrierInterface
{
private const CARRIER_CODE = 'instore';
private const METHOD_CODE = 'pickup';
public const DELIVERY_METHOD = self::CARRIER_CODE . '_' . self::METHOD_CODE;
/**
* @var bool
*/
protected $_isFixed = true;
/**
* @var ResultFactory
*/
private $rateResultFactory;
/**
* @var MethodFactory
*/
private $rateMethodFactory;
/**
* @var GetShippingPriceInterface
*/
private $getShippingPrice;
/**
* @var RequestValidatorInterface
*/
private $requestValidator;
/**
* @var GetCarrierTitle
*/
private $getCarrierTitle;
/**
* @param ScopeConfigInterface $scopeConfig
* @param ErrorFactory $rateErrorFactory
* @param LoggerInterface $logger
* @param ResultFactory $rateResultFactory
* @param MethodFactory $rateMethodFactory
* @param GetShippingPriceInterface $getShippingPrice
* @param RequestValidatorInterface $requestValidator
* @param GetCarrierTitle $getCarrierTitle
* @param array $data
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
ErrorFactory $rateErrorFactory,
LoggerInterface $logger,
ResultFactory $rateResultFactory,
MethodFactory $rateMethodFactory,
GetShippingPriceInterface $getShippingPrice,
RequestValidatorInterface $requestValidator,
GetCarrierTitle $getCarrierTitle,
array $data = []
) {
$this->rateResultFactory = $rateResultFactory;
$this->rateMethodFactory = $rateMethodFactory;
$this->getShippingPrice = $getShippingPrice;
$this->requestValidator = $requestValidator;
$this->getCarrierTitle = $getCarrierTitle;
$this->_code = self::CARRIER_CODE;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}
/**
* @inheritdoc
*/
public function processAdditionalValidation(DataObject $request)
{
/** @var RateRequest $request */
$validationResult = $this->requestValidator->validate($request);
if (!$validationResult->isValid()) {
return $this->createErrorResult();
}
return $validationResult->isValid();
}
/**
* Build shipping method error message.
*
* @return Error
*/
private function createErrorResult(): Error
{
return $this->_rateErrorFactory->create(
[
'data' => [
'error_message' => $this->getConfigData('specificerrmsg')
]
]
);
}
/**
* @inheritdoc
*/
public function collectRates(RateRequest $request)
{
if (!$this->isActive()) {
return null;
}
$shippingPrice = $this->getShippingPrice->execute($request);
$result = $this->rateResultFactory->create();
$method = $this->createResultMethod($shippingPrice);
$result->append($method);
return $result;
}
/**
* Create rate object based on shipping price.
*
* @param float $shippingPrice
* @return Method
*/
private function createResultMethod(float $shippingPrice): Method
{
$store = $this->getStore();
if ($store instanceof StoreInterface) {
$store = $store->getId();
}
$method = $this->rateMethodFactory->create(
[
'data' => [
'carrier' => self::CARRIER_CODE,
'carrier_title' => $this->getCarrierTitle->execute((int)$store),
'method' => self::METHOD_CODE,
'method_title' => $this->getConfigData('name'),
'cost' => $shippingPrice
]
]
);
$method->setPrice($shippingPrice);
return $method;
}
/**
* @inheritdoc
*/
public function getAllowedMethods()
{
return [$this->_code => $this->getConfigData('name')];
}
}