-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathOperation.php
216 lines (193 loc) · 5.96 KB
/
Operation.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace ShoppingFeed\Sdk\Api\Order;
use ArrayAccess;
use ArrayObject;
use Exception;
use ShoppingFeed\Sdk\Api\Order\Identifier\OrderIdentifier;
use ShoppingFeed\Sdk\Exception\InvalidArgumentException;
use ShoppingFeed\Sdk\Hal;
use ShoppingFeed\Sdk\Operation\AbstractBulkOperation;
use ShoppingFeed\Sdk\Operation\OperationInterface;
final class Operation extends AbstractBulkOperation implements OperationInterface
{
/**
* Operation types
*/
private const TYPE_ACCEPT = 'accept';
private const TYPE_CANCEL = 'cancel';
private const TYPE_REFUSE = 'refuse';
private const TYPE_SHIP = 'ship';
private const TYPE_REFUND = 'refund';
private const TYPE_ACKNOWLEDGE = 'acknowledge';
private const TYPE_UNACKNOWLEDGE = 'unacknowledge';
private const TYPE_UPLOAD_DOCUMENTS = 'upload-documents';
private const TYPE_DELIVER = 'deliver';
/**
* @var string[]
*/
private $allowedOperationTypes = [
self::TYPE_ACCEPT,
self::TYPE_CANCEL,
self::TYPE_REFUSE,
self::TYPE_SHIP,
self::TYPE_REFUND,
self::TYPE_ACKNOWLEDGE,
self::TYPE_UNACKNOWLEDGE,
self::TYPE_UPLOAD_DOCUMENTS,
self::TYPE_DELIVER,
];
/**
* Notify marketplace of order acceptance
*
* @throws InvalidArgumentException
*/
public function accept(OrderIdentifier $identifier, string $reason = ''): self
{
return $this->addOperation($identifier, self::TYPE_ACCEPT, compact('reason'));
}
/**
* Notify marketplace of order refusal
*
* @throws InvalidArgumentException
*/
public function refuse(OrderIdentifier $identifier): self
{
return $this->addOperation($identifier, self::TYPE_REFUSE);
}
/**
* Notify marketplace of order shipment sent
*
* @throws InvalidArgumentException
*/
public function ship(
OrderIdentifier $identifier,
string $carrier = '',
string $trackingNumber = '',
string $trackingLink = '',
array $items = []
): self
{
return $this->addOperation(
$identifier,
self::TYPE_SHIP,
compact('carrier', 'trackingNumber', 'trackingLink', 'items')
);
}
/**
* Notify marketplace of order cancellation
*
* @throws InvalidArgumentException
*/
public function cancel(OrderIdentifier $identifier, string $reason = ''): self
{
return $this->addOperation($identifier, self::TYPE_CANCEL, compact('reason'));
}
/**
* Notify marketplace of order refund
*
* @throws InvalidArgumentException
*/
public function refund(OrderIdentifier $identifier, bool $shipping = true, array $products = []): self
{
return $this->addOperation(
$identifier,
self::TYPE_REFUND,
['refund' => compact('shipping', 'products')]
);
}
/**
* Acknowledge order reception
*
* @throws InvalidArgumentException
* @throws Exception
*/
public function acknowledge(
OrderIdentifier $identifier,
string $storeReference = '',
string $status = 'success',
string $message = ''
): self
{
$acknowledgedAt = date_create()->format('c');
return $this->addOperation(
$identifier,
self::TYPE_ACKNOWLEDGE,
compact('status', 'storeReference', 'acknowledgedAt', 'message')
);
}
/**
* Unacknowledge order reception
*
* @throws InvalidArgumentException
* @throws Exception
*/
public function unacknowledge(OrderIdentifier $identifier): self
{
return $this->addOperation($identifier, self::TYPE_UNACKNOWLEDGE);
}
/**
* Upload document
*
* @throws InvalidArgumentException
*/
public function uploadDocument(OrderIdentifier $identifier, Document\AbstractDocument $document): self
{
return $this->addOperation($identifier, self::TYPE_UPLOAD_DOCUMENTS, ['document' => $document]);
}
/**
* Notify marketplace of order delivery
*
* @throws InvalidArgumentException
*/
public function deliver(OrderIdentifier $identifier): self
{
return $this->addOperation($identifier, self::TYPE_DELIVER);
}
private function addOperation(OrderIdentifier $identifier, string $type, array $data = []): self
{
if (! in_array($type, $this->allowedOperationTypes)) {
throw new InvalidArgumentException(sprintf(
'Only %s operations are accepted',
implode(', ', $this->allowedOperationTypes)
));
}
if (! isset($this->operations[$type])) {
$this->operations[$type] = [];
}
$this->operations[$type][] = array_merge($identifier->toArray(), $data);
return $this;
}
public function execute(Hal\HalLink $link): OrderOperationResult
{
$requests = new ArrayObject();
$resources = new ArrayObject();
foreach ($this->allowedOperationTypes as $type) {
$this->populateRequests($type, $link, $requests);
}
$link->batchSend(
$requests->getArrayCopy(),
function (Hal\HalResource $batch) use ($resources) {
$resources->append($batch);
},
null,
[],
$this->getPoolSize()
);
return new OrderOperationResult(
$resources->getArrayCopy()
);
}
private function populateRequests(string $type, Hal\HalLink $link, ArrayAccess $requests): void
{
$this->eachBatch(
function (array $chunk) use ($type, $link, &$requests) {
$requests[] = $link->createRequest(
'POST',
['operation' => $type],
['order' => $chunk]
);
},
$type
);
}
}