Passed
Push — develop ( f53d0b...3a55e6 )
by Jens
09:08
created

OrdersActionBuilder::getActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Commercetools\Core\Builder\Update;
4
5
use Commercetools\Core\Error\InvalidArgumentException;
6
use Commercetools\Core\Request\AbstractAction;
7
use Commercetools\Core\Request\Orders\Command\OrderAddDeliveryAction;
8
use Commercetools\Core\Request\Orders\Command\OrderAddParcelToDeliveryAction;
9
use Commercetools\Core\Request\Orders\Command\OrderAddPaymentAction;
10
use Commercetools\Core\Request\Orders\Command\OrderAddReturnInfoAction;
11
use Commercetools\Core\Request\Orders\Command\OrderChangeOrderStateAction;
12
use Commercetools\Core\Request\Orders\Command\OrderChangePaymentStateAction;
13
use Commercetools\Core\Request\Orders\Command\OrderChangeShipmentStateAction;
14
use Commercetools\Core\Request\Orders\Command\OrderImportCustomLineItemStateAction;
15
use Commercetools\Core\Request\Orders\Command\OrderImportLineItemStateAction;
16
use Commercetools\Core\Request\Orders\Command\OrderRemoveDeliveryAction;
17
use Commercetools\Core\Request\Orders\Command\OrderRemoveParcelFromDeliveryAction;
18
use Commercetools\Core\Request\Orders\Command\OrderRemovePaymentAction;
19
use Commercetools\Core\Request\Orders\Command\OrderSetBillingAddress;
20
use Commercetools\Core\Request\Orders\Command\OrderSetCustomFieldAction;
21
use Commercetools\Core\Request\Orders\Command\OrderSetCustomLineItemCustomFieldAction;
22
use Commercetools\Core\Request\Orders\Command\OrderSetCustomLineItemCustomTypeAction;
23
use Commercetools\Core\Request\Orders\Command\OrderSetCustomTypeAction;
24
use Commercetools\Core\Request\Orders\Command\OrderSetCustomerEmail;
25
use Commercetools\Core\Request\Orders\Command\OrderSetDeliveryAddressAction;
26
use Commercetools\Core\Request\Orders\Command\OrderSetDeliveryItemsAction;
27
use Commercetools\Core\Request\Orders\Command\OrderSetLineItemCustomFieldAction;
28
use Commercetools\Core\Request\Orders\Command\OrderSetLineItemCustomTypeAction;
29
use Commercetools\Core\Request\Orders\Command\OrderSetLocaleAction;
30
use Commercetools\Core\Request\Orders\Command\OrderSetOrderNumberAction;
31
use Commercetools\Core\Request\Orders\Command\OrderSetParcelItemsAction;
32
use Commercetools\Core\Request\Orders\Command\OrderSetParcelMeasurementsAction;
33
use Commercetools\Core\Request\Orders\Command\OrderSetParcelTrackingDataAction;
34
use Commercetools\Core\Request\Orders\Command\OrderSetReturnPaymentStateAction;
35
use Commercetools\Core\Request\Orders\Command\OrderSetReturnShipmentStateAction;
36
use Commercetools\Core\Request\Orders\Command\OrderSetShippingAddress;
37
use Commercetools\Core\Request\Orders\Command\OrderTransitionCustomLineItemStateAction;
38
use Commercetools\Core\Request\Orders\Command\OrderTransitionLineItemStateAction;
39
use Commercetools\Core\Request\Orders\Command\OrderTransitionStateAction;
40
use Commercetools\Core\Request\Orders\Command\OrderUpdateSyncInfoAction;
41
42
class OrdersActionBuilder
43
{
44
    private $actions = [];
45
46
    /**
47
     * @link https://docs.commercetools.com/http-api-projects-orders.html#add-delivery
48
     * @param OrderAddDeliveryAction|callable $action
49
     * @return $this
50
     */
51 1
    public function addDelivery($action = null)
52
    {
53 1
        $this->addAction($this->resolveAction(OrderAddDeliveryAction::class, $action));
54 1
        return $this;
55
    }
56
57
    /**
58
     * @link https://docs.commercetools.com/http-api-projects-orders.html#add-parcel
59
     * @param OrderAddParcelToDeliveryAction|callable $action
60
     * @return $this
61
     */
62 1
    public function addParcelToDelivery($action = null)
63
    {
64 1
        $this->addAction($this->resolveAction(OrderAddParcelToDeliveryAction::class, $action));
65 1
        return $this;
66
    }
67
68
    /**
69
     * @link https://docs.commercetools.com/http-api-projects-orders.html#add-payment
70
     * @param OrderAddPaymentAction|callable $action
71
     * @return $this
72
     */
73 1
    public function addPayment($action = null)
74
    {
75 1
        $this->addAction($this->resolveAction(OrderAddPaymentAction::class, $action));
76 1
        return $this;
77
    }
78
79
    /**
80
     * @link https://docs.commercetools.com/http-api-projects-orders.html#addreturninfo
81
     * @param OrderAddReturnInfoAction|callable $action
82
     * @return $this
83
     */
84 1
    public function addReturnInfo($action = null)
85
    {
86 1
        $this->addAction($this->resolveAction(OrderAddReturnInfoAction::class, $action));
87 1
        return $this;
88
    }
89
90
    /**
91
     * @link https://docs.commercetools.com/http-api-projects-orders.html#change-orderstate
92
     * @param OrderChangeOrderStateAction|callable $action
93
     * @return $this
94
     */
95 1
    public function changeOrderState($action = null)
96
    {
97 1
        $this->addAction($this->resolveAction(OrderChangeOrderStateAction::class, $action));
98 1
        return $this;
99
    }
100
101
    /**
102
     * @link https://docs.commercetools.com/http-api-projects-orders.html#change-paymentstate
103
     * @param OrderChangePaymentStateAction|callable $action
104
     * @return $this
105
     */
106 1
    public function changePaymentState($action = null)
107
    {
108 1
        $this->addAction($this->resolveAction(OrderChangePaymentStateAction::class, $action));
109 1
        return $this;
110
    }
111
112
    /**
113
     * @link https://docs.commercetools.com/http-api-projects-orders.html#change-shipmentstate
114
     * @param OrderChangeShipmentStateAction|callable $action
115
     * @return $this
116
     */
117 1
    public function changeShipmentState($action = null)
118
    {
119 1
        $this->addAction($this->resolveAction(OrderChangeShipmentStateAction::class, $action));
120 1
        return $this;
121
    }
122
123
    /**
124
     * @link https://docs.commercetools.com/http-api-projects-orders.html#import-state-for-customlineitems
125
     * @param OrderImportCustomLineItemStateAction|callable $action
126
     * @return $this
127
     */
128 1
    public function importCustomLineItemState($action = null)
129
    {
130 1
        $this->addAction($this->resolveAction(OrderImportCustomLineItemStateAction::class, $action));
131 1
        return $this;
132
    }
133
134
    /**
135
     * @link https://docs.commercetools.com/http-api-projects-orders.html#import-state-for-lineitems
136
     * @param OrderImportLineItemStateAction|callable $action
137
     * @return $this
138
     */
139 1
    public function importLineItemState($action = null)
140
    {
141 1
        $this->addAction($this->resolveAction(OrderImportLineItemStateAction::class, $action));
142 1
        return $this;
143
    }
144
145
    /**
146
     * @link https://docs.commercetools.com/http-api-projects-orders.html#remove-delivery
147
     * @param OrderRemoveDeliveryAction|callable $action
148
     * @return $this
149
     */
150 1
    public function removeDelivery($action = null)
151
    {
152 1
        $this->addAction($this->resolveAction(OrderRemoveDeliveryAction::class, $action));
153 1
        return $this;
154
    }
155
156
    /**
157
     * @link https://docs.commercetools.com/http-api-projects-orders.html#remove-parcel-from-delivery
158
     * @param OrderRemoveParcelFromDeliveryAction|callable $action
159
     * @return $this
160
     */
161 1
    public function removeParcelFromDelivery($action = null)
162
    {
163 1
        $this->addAction($this->resolveAction(OrderRemoveParcelFromDeliveryAction::class, $action));
164 1
        return $this;
165
    }
166
167
    /**
168
     * @link https://docs.commercetools.com/http-api-projects-orders.html#remove-payment
169
     * @param OrderRemovePaymentAction|callable $action
170
     * @return $this
171
     */
172 1
    public function removePayment($action = null)
173
    {
174 1
        $this->addAction($this->resolveAction(OrderRemovePaymentAction::class, $action));
175 1
        return $this;
176
    }
177
178
    /**
179
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-billing-address
180
     * @param OrderSetBillingAddress|callable $action
181
     * @return $this
182
     */
183 1
    public function setBillingAddress($action = null)
184
    {
185 1
        $this->addAction($this->resolveAction(OrderSetBillingAddress::class, $action));
186 1
        return $this;
187
    }
188
189
    /**
190
     *
191
     * @param OrderSetCustomFieldAction|callable $action
192
     * @return $this
193
     */
194 1
    public function setCustomField($action = null)
195
    {
196 1
        $this->addAction($this->resolveAction(OrderSetCustomFieldAction::class, $action));
197 1
        return $this;
198
    }
199
200
    /**
201
     *
202
     * @param OrderSetCustomLineItemCustomFieldAction|callable $action
203
     * @return $this
204
     */
205 1
    public function setCustomLineItemCustomField($action = null)
206
    {
207 1
        $this->addAction($this->resolveAction(OrderSetCustomLineItemCustomFieldAction::class, $action));
208 1
        return $this;
209
    }
210
211
    /**
212
     *
213
     * @param OrderSetCustomLineItemCustomTypeAction|callable $action
214
     * @return $this
215
     */
216 1
    public function setCustomLineItemCustomType($action = null)
217
    {
218 1
        $this->addAction($this->resolveAction(OrderSetCustomLineItemCustomTypeAction::class, $action));
219 1
        return $this;
220
    }
221
222
    /**
223
     *
224
     * @param OrderSetCustomTypeAction|callable $action
225
     * @return $this
226
     */
227 1
    public function setCustomType($action = null)
228
    {
229 1
        $this->addAction($this->resolveAction(OrderSetCustomTypeAction::class, $action));
230 1
        return $this;
231
    }
232
233
    /**
234
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-customer-email
235
     * @param OrderSetCustomerEmail|callable $action
236
     * @return $this
237
     */
238 1
    public function setCustomerEmail($action = null)
239
    {
240 1
        $this->addAction($this->resolveAction(OrderSetCustomerEmail::class, $action));
241 1
        return $this;
242
    }
243
244
    /**
245
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-delivery-address
246
     * @param OrderSetDeliveryAddressAction|callable $action
247
     * @return $this
248
     */
249 1
    public function setDeliveryAddress($action = null)
250
    {
251 1
        $this->addAction($this->resolveAction(OrderSetDeliveryAddressAction::class, $action));
252 1
        return $this;
253
    }
254
255
    /**
256
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-delivery-items
257
     * @param OrderSetDeliveryItemsAction|callable $action
258
     * @return $this
259
     */
260 1
    public function setDeliveryItems($action = null)
261
    {
262 1
        $this->addAction($this->resolveAction(OrderSetDeliveryItemsAction::class, $action));
263 1
        return $this;
264
    }
265
266
    /**
267
     *
268
     * @param OrderSetLineItemCustomFieldAction|callable $action
269
     * @return $this
270
     */
271 1
    public function setLineItemCustomField($action = null)
272
    {
273 1
        $this->addAction($this->resolveAction(OrderSetLineItemCustomFieldAction::class, $action));
274 1
        return $this;
275
    }
276
277
    /**
278
     *
279
     * @param OrderSetLineItemCustomTypeAction|callable $action
280
     * @return $this
281
     */
282 1
    public function setLineItemCustomType($action = null)
283
    {
284 1
        $this->addAction($this->resolveAction(OrderSetLineItemCustomTypeAction::class, $action));
285 1
        return $this;
286
    }
287
288
    /**
289
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-locale
290
     * @param OrderSetLocaleAction|callable $action
291
     * @return $this
292
     */
293 1
    public function setLocale($action = null)
294
    {
295 1
        $this->addAction($this->resolveAction(OrderSetLocaleAction::class, $action));
296 1
        return $this;
297
    }
298
299
    /**
300
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-order-number
301
     * @param OrderSetOrderNumberAction|callable $action
302
     * @return $this
303
     */
304 1
    public function setOrderNumber($action = null)
305
    {
306 1
        $this->addAction($this->resolveAction(OrderSetOrderNumberAction::class, $action));
307 1
        return $this;
308
    }
309
310
    /**
311
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-parcel-items
312
     * @param OrderSetParcelItemsAction|callable $action
313
     * @return $this
314
     */
315 1
    public function setParcelItems($action = null)
316
    {
317 1
        $this->addAction($this->resolveAction(OrderSetParcelItemsAction::class, $action));
318 1
        return $this;
319
    }
320
321
    /**
322
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-parcel-measurements
323
     * @param OrderSetParcelMeasurementsAction|callable $action
324
     * @return $this
325
     */
326 1
    public function setParcelMeasurements($action = null)
327
    {
328 1
        $this->addAction($this->resolveAction(OrderSetParcelMeasurementsAction::class, $action));
329 1
        return $this;
330
    }
331
332
    /**
333
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-parcel-tracking-data
334
     * @param OrderSetParcelTrackingDataAction|callable $action
335
     * @return $this
336
     */
337 1
    public function setParcelTrackingData($action = null)
338
    {
339 1
        $this->addAction($this->resolveAction(OrderSetParcelTrackingDataAction::class, $action));
340 1
        return $this;
341
    }
342
343
    /**
344
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-returnpaymentstate
345
     * @param OrderSetReturnPaymentStateAction|callable $action
346
     * @return $this
347
     */
348 1
    public function setReturnPaymentState($action = null)
349
    {
350 1
        $this->addAction($this->resolveAction(OrderSetReturnPaymentStateAction::class, $action));
351 1
        return $this;
352
    }
353
354
    /**
355
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-returnshipmentstate
356
     * @param OrderSetReturnShipmentStateAction|callable $action
357
     * @return $this
358
     */
359 1
    public function setReturnShipmentState($action = null)
360
    {
361 1
        $this->addAction($this->resolveAction(OrderSetReturnShipmentStateAction::class, $action));
362 1
        return $this;
363
    }
364
365
    /**
366
     * @link https://docs.commercetools.com/http-api-projects-orders.html#set-shipping-address
367
     * @param OrderSetShippingAddress|callable $action
368
     * @return $this
369
     */
370 1
    public function setShippingAddress($action = null)
371
    {
372 1
        $this->addAction($this->resolveAction(OrderSetShippingAddress::class, $action));
373 1
        return $this;
374
    }
375
376
    /**
377
     * @link https://docs.commercetools.com/http-api-projects-orders.html#change-the-state-of-customlineitem-according-to-allowed-transitions
378
     * @param OrderTransitionCustomLineItemStateAction|callable $action
379
     * @return $this
380
     */
381 1
    public function transitionCustomLineItemState($action = null)
382
    {
383 1
        $this->addAction($this->resolveAction(OrderTransitionCustomLineItemStateAction::class, $action));
384 1
        return $this;
385
    }
386
387
    /**
388
     * @link https://docs.commercetools.com/http-api-projects-orders.html#change-the-state-of-lineitem-according-to-allowed-transitions
389
     * @param OrderTransitionLineItemStateAction|callable $action
390
     * @return $this
391
     */
392 1
    public function transitionLineItemState($action = null)
393
    {
394 1
        $this->addAction($this->resolveAction(OrderTransitionLineItemStateAction::class, $action));
395 1
        return $this;
396
    }
397
398
    /**
399
     * @link https://docs.commercetools.com/http-api-projects-orders.html#transition-state
400
     * @param OrderTransitionStateAction|callable $action
401
     * @return $this
402
     */
403 1
    public function transitionState($action = null)
404
    {
405 1
        $this->addAction($this->resolveAction(OrderTransitionStateAction::class, $action));
406 1
        return $this;
407
    }
408
409
    /**
410
     * @link https://docs.commercetools.com/http-api-projects-orders.html#update-syncinfo
411
     * @param OrderUpdateSyncInfoAction|callable $action
412
     * @return $this
413
     */
414 1
    public function updateSyncInfo($action = null)
415
    {
416 1
        $this->addAction($this->resolveAction(OrderUpdateSyncInfoAction::class, $action));
417 1
        return $this;
418
    }
419
420
    /**
421
     * @return OrdersActionBuilder
422
     */
423
    public function of()
424
    {
425
        return new self();
426
    }
427
428
    /**
429
     * @param $class
430
     * @param $action
431
     * @return AbstractAction
432
     * @throws InvalidArgumentException
433
     */
434 34
    private function resolveAction($class, $action = null)
435
    {
436 34
        if (is_null($action) || is_callable($action)) {
437 34
            $callback = $action;
438 34
            $emptyAction = $class::of();
439 34
            $action = $this->callback($emptyAction, $callback);
440
        }
441 34
        if ($action instanceof $class) {
442 34
            return $action;
443
        }
444
        throw new InvalidArgumentException(
445
            sprintf('Expected method to be called with or callable to return %s', $class)
446
        );
447
    }
448
449
    /**
450
     * @param $action
451
     * @param callable $callback
452
     * @return AbstractAction
453
     */
454 34
    private function callback($action, callable $callback = null)
455
    {
456 34
        if (!is_null($callback)) {
457
            $action = $callback($action);
458
        }
459 34
        return $action;
460
    }
461
462
    /**
463
     * @param AbstractAction $action
464
     * @return $this;
465
     */
466 34
    public function addAction(AbstractAction $action)
467
    {
468 34
        $this->actions[] = $action;
469 34
        return $this;
470
    }
471
472
    /**
473
     * @return array
474
     */
475 34
    public function getActions()
476
    {
477 34
        return $this->actions;
478
    }
479
480
    /**
481
     * @param array $actions
482
     * @return $this
483
     */
484
    public function setActions(array $actions)
485
    {
486
        $this->actions = $actions;
487
        return $this;
488
    }
489
}
490