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

CartsActionBuilder::resolveAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 7
cts 9
cp 0.7778
rs 9.2
cc 4
eloc 8
nc 4
nop 2
crap 4.1755
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\Carts\Command\CartAddCustomLineItemAction;
8
use Commercetools\Core\Request\Carts\Command\CartAddDiscountCodeAction;
9
use Commercetools\Core\Request\Carts\Command\CartAddLineItemAction;
10
use Commercetools\Core\Request\Carts\Command\CartAddPaymentAction;
11
use Commercetools\Core\Request\Carts\Command\CartAddShoppingListAction;
12
use Commercetools\Core\Request\Carts\Command\CartChangeCustomLineItemMoneyAction;
13
use Commercetools\Core\Request\Carts\Command\CartChangeCustomLineItemQuantityAction;
14
use Commercetools\Core\Request\Carts\Command\CartChangeLineItemQuantityAction;
15
use Commercetools\Core\Request\Carts\Command\CartChangeTaxCalculationModeAction;
16
use Commercetools\Core\Request\Carts\Command\CartChangeTaxModeAction;
17
use Commercetools\Core\Request\Carts\Command\CartChangeTaxRoundingModeAction;
18
use Commercetools\Core\Request\Carts\Command\CartRecalculateAction;
19
use Commercetools\Core\Request\Carts\Command\CartRemoveCustomLineItemAction;
20
use Commercetools\Core\Request\Carts\Command\CartRemoveDiscountCodeAction;
21
use Commercetools\Core\Request\Carts\Command\CartRemoveLineItemAction;
22
use Commercetools\Core\Request\Carts\Command\CartRemovePaymentAction;
23
use Commercetools\Core\Request\Carts\Command\CartSetAnonymousIdAction;
24
use Commercetools\Core\Request\Carts\Command\CartSetBillingAddressAction;
25
use Commercetools\Core\Request\Carts\Command\CartSetCartTotalTaxAction;
26
use Commercetools\Core\Request\Carts\Command\CartSetCountryAction;
27
use Commercetools\Core\Request\Carts\Command\CartSetCustomFieldAction;
28
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemCustomFieldAction;
29
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemCustomTypeAction;
30
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemTaxAmountAction;
31
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemTaxRateAction;
32
use Commercetools\Core\Request\Carts\Command\CartSetCustomShippingMethodAction;
33
use Commercetools\Core\Request\Carts\Command\CartSetCustomTypeAction;
34
use Commercetools\Core\Request\Carts\Command\CartSetCustomerEmailAction;
35
use Commercetools\Core\Request\Carts\Command\CartSetCustomerGroupAction;
36
use Commercetools\Core\Request\Carts\Command\CartSetCustomerIdAction;
37
use Commercetools\Core\Request\Carts\Command\CartSetDeleteDaysAfterLastModificationAction;
38
use Commercetools\Core\Request\Carts\Command\CartSetLineItemCustomFieldAction;
39
use Commercetools\Core\Request\Carts\Command\CartSetLineItemCustomTypeAction;
40
use Commercetools\Core\Request\Carts\Command\CartSetLineItemPriceAction;
41
use Commercetools\Core\Request\Carts\Command\CartSetLineItemTaxAmountAction;
42
use Commercetools\Core\Request\Carts\Command\CartSetLineItemTaxRateAction;
43
use Commercetools\Core\Request\Carts\Command\CartSetLineItemTotalPriceAction;
44
use Commercetools\Core\Request\Carts\Command\CartSetLocaleAction;
45
use Commercetools\Core\Request\Carts\Command\CartSetShippingAddressAction;
46
use Commercetools\Core\Request\Carts\Command\CartSetShippingMethodAction;
47
use Commercetools\Core\Request\Carts\Command\CartSetShippingMethodTaxAmountAction;
48
use Commercetools\Core\Request\Carts\Command\CartSetShippingMethodTaxRateAction;
49
use Commercetools\Core\Request\Carts\Command\CartSetShippingRateInputAction;
50
51
class CartsActionBuilder
52
{
53
    private $actions = [];
54
55
    /**
56
     * @link https://docs.commercetools.com/http-api-projects-carts.html#add-customlineitem
57
     * @param CartAddCustomLineItemAction|callable $action
58
     * @return $this
59
     */
60 1
    public function addCustomLineItem($action = null)
61
    {
62 1
        $this->addAction($this->resolveAction(CartAddCustomLineItemAction::class, $action));
63 1
        return $this;
64
    }
65
66
    /**
67
     * @link https://docs.commercetools.com/http-api-projects-carts.html#add-discountcode
68
     * @param CartAddDiscountCodeAction|callable $action
69
     * @return $this
70
     */
71 1
    public function addDiscountCode($action = null)
72
    {
73 1
        $this->addAction($this->resolveAction(CartAddDiscountCodeAction::class, $action));
74 1
        return $this;
75
    }
76
77
    /**
78
     * @link https://docs.commercetools.com/http-api-projects-carts.html#add-lineitem
79
     * @param CartAddLineItemAction|callable $action
80
     * @return $this
81
     */
82 1
    public function addLineItem($action = null)
83
    {
84 1
        $this->addAction($this->resolveAction(CartAddLineItemAction::class, $action));
85 1
        return $this;
86
    }
87
88
    /**
89
     * @link https://docs.commercetools.com/http-api-projects-carts.html#add-payment
90
     * @param CartAddPaymentAction|callable $action
91
     * @return $this
92
     */
93 1
    public function addPayment($action = null)
94
    {
95 1
        $this->addAction($this->resolveAction(CartAddPaymentAction::class, $action));
96 1
        return $this;
97
    }
98
99
    /**
100
     * @link https://docs.commercetools.com/http-api-projects-carts.html#add-shoppinglist
101
     * @param CartAddShoppingListAction|callable $action
102
     * @return $this
103
     */
104 1
    public function addShoppingList($action = null)
105
    {
106 1
        $this->addAction($this->resolveAction(CartAddShoppingListAction::class, $action));
107 1
        return $this;
108
    }
109
110
    /**
111
     * @link https://docs.commercetools.com/http-api-projects-carts.html#change-customlineitem-money
112
     * @param CartChangeCustomLineItemMoneyAction|callable $action
113
     * @return $this
114
     */
115 1
    public function changeCustomLineItemMoney($action = null)
116
    {
117 1
        $this->addAction($this->resolveAction(CartChangeCustomLineItemMoneyAction::class, $action));
118 1
        return $this;
119
    }
120
121
    /**
122
     * @link https://docs.commercetools.com/http-api-projects-carts.html#change-customlineitem-quantity
123
     * @param CartChangeCustomLineItemQuantityAction|callable $action
124
     * @return $this
125
     */
126 1
    public function changeCustomLineItemQuantity($action = null)
127
    {
128 1
        $this->addAction($this->resolveAction(CartChangeCustomLineItemQuantityAction::class, $action));
129 1
        return $this;
130
    }
131
132
    /**
133
     * @link https://docs.commercetools.com/http-api-projects-carts.html#change-lineitem-quantity
134
     * @param CartChangeLineItemQuantityAction|callable $action
135
     * @return $this
136
     */
137 1
    public function changeLineItemQuantity($action = null)
138
    {
139 1
        $this->addAction($this->resolveAction(CartChangeLineItemQuantityAction::class, $action));
140 1
        return $this;
141
    }
142
143
    /**
144
     * @link https://docs.commercetools.com/http-api-projects-carts.html#change-tax-calculationmode
145
     * @param CartChangeTaxCalculationModeAction|callable $action
146
     * @return $this
147
     */
148 1
    public function changeTaxCalculationMode($action = null)
149
    {
150 1
        $this->addAction($this->resolveAction(CartChangeTaxCalculationModeAction::class, $action));
151 1
        return $this;
152
    }
153
154
    /**
155
     * @link https://docs.commercetools.com/http-api-projects-carts.html#change-taxmode
156
     * @param CartChangeTaxModeAction|callable $action
157
     * @return $this
158
     */
159 1
    public function changeTaxMode($action = null)
160
    {
161 1
        $this->addAction($this->resolveAction(CartChangeTaxModeAction::class, $action));
162 1
        return $this;
163
    }
164
165
    /**
166
     * @link https://docs.commercetools.com/http-api-projects-carts.html#change-tax-roundingmode
167
     * @param CartChangeTaxRoundingModeAction|callable $action
168
     * @return $this
169
     */
170 1
    public function changeTaxRoundingMode($action = null)
171
    {
172 1
        $this->addAction($this->resolveAction(CartChangeTaxRoundingModeAction::class, $action));
173 1
        return $this;
174
    }
175
176
    /**
177
     * @link https://docs.commercetools.com/http-api-projects-carts.html#recalculate
178
     * @param CartRecalculateAction|callable $action
179
     * @return $this
180
     */
181 1
    public function recalculate($action = null)
182
    {
183 1
        $this->addAction($this->resolveAction(CartRecalculateAction::class, $action));
184 1
        return $this;
185
    }
186
187
    /**
188
     * @link https://docs.commercetools.com/http-api-projects-carts.html#remove-customlineitem
189
     * @param CartRemoveCustomLineItemAction|callable $action
190
     * @return $this
191
     */
192 1
    public function removeCustomLineItem($action = null)
193
    {
194 1
        $this->addAction($this->resolveAction(CartRemoveCustomLineItemAction::class, $action));
195 1
        return $this;
196
    }
197
198
    /**
199
     * @link https://docs.commercetools.com/http-api-projects-carts.html#remove-discountcode
200
     * @param CartRemoveDiscountCodeAction|callable $action
201
     * @return $this
202
     */
203 1
    public function removeDiscountCode($action = null)
204
    {
205 1
        $this->addAction($this->resolveAction(CartRemoveDiscountCodeAction::class, $action));
206 1
        return $this;
207
    }
208
209
    /**
210
     * @link https://docs.commercetools.com/http-api-projects-carts.html#remove-lineitem
211
     * @param CartRemoveLineItemAction|callable $action
212
     * @return $this
213
     */
214 1
    public function removeLineItem($action = null)
215
    {
216 1
        $this->addAction($this->resolveAction(CartRemoveLineItemAction::class, $action));
217 1
        return $this;
218
    }
219
220
    /**
221
     * @link https://docs.commercetools.com/http-api-projects-carts.html#remove-payment
222
     * @param CartRemovePaymentAction|callable $action
223
     * @return $this
224
     */
225 1
    public function removePayment($action = null)
226
    {
227 1
        $this->addAction($this->resolveAction(CartRemovePaymentAction::class, $action));
228 1
        return $this;
229
    }
230
231
    /**
232
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-anonymous-id
233
     * @param CartSetAnonymousIdAction|callable $action
234
     * @return $this
235
     */
236 1
    public function setAnonymousId($action = null)
237
    {
238 1
        $this->addAction($this->resolveAction(CartSetAnonymousIdAction::class, $action));
239 1
        return $this;
240
    }
241
242
    /**
243
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-billing-address
244
     * @param CartSetBillingAddressAction|callable $action
245
     * @return $this
246
     */
247 1
    public function setBillingAddress($action = null)
248
    {
249 1
        $this->addAction($this->resolveAction(CartSetBillingAddressAction::class, $action));
250 1
        return $this;
251
    }
252
253
    /**
254
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-cart-total-tax
255
     * @param CartSetCartTotalTaxAction|callable $action
256
     * @return $this
257
     */
258 1
    public function setCartTotalTax($action = null)
259
    {
260 1
        $this->addAction($this->resolveAction(CartSetCartTotalTaxAction::class, $action));
261 1
        return $this;
262
    }
263
264
    /**
265
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-country
266
     * @param CartSetCountryAction|callable $action
267
     * @return $this
268
     */
269 1
    public function setCountry($action = null)
270
    {
271 1
        $this->addAction($this->resolveAction(CartSetCountryAction::class, $action));
272 1
        return $this;
273
    }
274
275
    /**
276
     *
277
     * @param CartSetCustomFieldAction|callable $action
278
     * @return $this
279
     */
280 1
    public function setCustomField($action = null)
281
    {
282 1
        $this->addAction($this->resolveAction(CartSetCustomFieldAction::class, $action));
283 1
        return $this;
284
    }
285
286
    /**
287
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-customlineitem-customfield
288
     * @param CartSetCustomLineItemCustomFieldAction|callable $action
289
     * @return $this
290
     */
291 1
    public function setCustomLineItemCustomField($action = null)
292
    {
293 1
        $this->addAction($this->resolveAction(CartSetCustomLineItemCustomFieldAction::class, $action));
294 1
        return $this;
295
    }
296
297
    /**
298
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-customlineitem-custom-type
299
     * @param CartSetCustomLineItemCustomTypeAction|callable $action
300
     * @return $this
301
     */
302 1
    public function setCustomLineItemCustomType($action = null)
303
    {
304 1
        $this->addAction($this->resolveAction(CartSetCustomLineItemCustomTypeAction::class, $action));
305 1
        return $this;
306
    }
307
308
    /**
309
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-customlineitem-taxamount
310
     * @param CartSetCustomLineItemTaxAmountAction|callable $action
311
     * @return $this
312
     */
313 1
    public function setCustomLineItemTaxAmount($action = null)
314
    {
315 1
        $this->addAction($this->resolveAction(CartSetCustomLineItemTaxAmountAction::class, $action));
316 1
        return $this;
317
    }
318
319
    /**
320
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-customlineitem-taxrate
321
     * @param CartSetCustomLineItemTaxRateAction|callable $action
322
     * @return $this
323
     */
324 1
    public function setCustomLineItemTaxRate($action = null)
325
    {
326 1
        $this->addAction($this->resolveAction(CartSetCustomLineItemTaxRateAction::class, $action));
327 1
        return $this;
328
    }
329
330
    /**
331
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-custom-shippingmethod
332
     * @param CartSetCustomShippingMethodAction|callable $action
333
     * @return $this
334
     */
335 1
    public function setCustomShippingMethod($action = null)
336
    {
337 1
        $this->addAction($this->resolveAction(CartSetCustomShippingMethodAction::class, $action));
338 1
        return $this;
339
    }
340
341
    /**
342
     *
343
     * @param CartSetCustomTypeAction|callable $action
344
     * @return $this
345
     */
346 1
    public function setCustomType($action = null)
347
    {
348 1
        $this->addAction($this->resolveAction(CartSetCustomTypeAction::class, $action));
349 1
        return $this;
350
    }
351
352
    /**
353
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-customer-email
354
     * @param CartSetCustomerEmailAction|callable $action
355
     * @return $this
356
     */
357 1
    public function setCustomerEmail($action = null)
358
    {
359 1
        $this->addAction($this->resolveAction(CartSetCustomerEmailAction::class, $action));
360 1
        return $this;
361
    }
362
363
    /**
364
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-customer-group
365
     * @param CartSetCustomerGroupAction|callable $action
366
     * @return $this
367
     */
368 1
    public function setCustomerGroup($action = null)
369
    {
370 1
        $this->addAction($this->resolveAction(CartSetCustomerGroupAction::class, $action));
371 1
        return $this;
372
    }
373
374
    /**
375
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-customer-id
376
     * @param CartSetCustomerIdAction|callable $action
377
     * @return $this
378
     */
379 1
    public function setCustomerId($action = null)
380
    {
381 1
        $this->addAction($this->resolveAction(CartSetCustomerIdAction::class, $action));
382 1
        return $this;
383
    }
384
385
    /**
386
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-deletedaysafterlastmodification-beta
387
     * @param CartSetDeleteDaysAfterLastModificationAction|callable $action
388
     * @return $this
389
     */
390 1
    public function setDeleteDaysAfterLastModification($action = null)
391
    {
392 1
        $this->addAction($this->resolveAction(CartSetDeleteDaysAfterLastModificationAction::class, $action));
393 1
        return $this;
394
    }
395
396
    /**
397
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-lineitem-customfield
398
     * @param CartSetLineItemCustomFieldAction|callable $action
399
     * @return $this
400
     */
401 1
    public function setLineItemCustomField($action = null)
402
    {
403 1
        $this->addAction($this->resolveAction(CartSetLineItemCustomFieldAction::class, $action));
404 1
        return $this;
405
    }
406
407
    /**
408
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-customlineitem-custom-type
409
     * @param CartSetLineItemCustomTypeAction|callable $action
410
     * @return $this
411
     */
412 1
    public function setLineItemCustomType($action = null)
413
    {
414 1
        $this->addAction($this->resolveAction(CartSetLineItemCustomTypeAction::class, $action));
415 1
        return $this;
416
    }
417
418
    /**
419
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-lineitem-totalprice
420
     * @param CartSetLineItemPriceAction|callable $action
421
     * @return $this
422
     */
423 1
    public function setLineItemPrice($action = null)
424
    {
425 1
        $this->addAction($this->resolveAction(CartSetLineItemPriceAction::class, $action));
426 1
        return $this;
427
    }
428
429
    /**
430
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-lineitem-taxamount
431
     * @param CartSetLineItemTaxAmountAction|callable $action
432
     * @return $this
433
     */
434 1
    public function setLineItemTaxAmount($action = null)
435
    {
436 1
        $this->addAction($this->resolveAction(CartSetLineItemTaxAmountAction::class, $action));
437 1
        return $this;
438
    }
439
440
    /**
441
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-lineitem-taxrate
442
     * @param CartSetLineItemTaxRateAction|callable $action
443
     * @return $this
444
     */
445 1
    public function setLineItemTaxRate($action = null)
446
    {
447 1
        $this->addAction($this->resolveAction(CartSetLineItemTaxRateAction::class, $action));
448 1
        return $this;
449
    }
450
451
    /**
452
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-lineitem-totalprice
453
     * @param CartSetLineItemTotalPriceAction|callable $action
454
     * @return $this
455
     */
456 1
    public function setLineItemTotalPrice($action = null)
457
    {
458 1
        $this->addAction($this->resolveAction(CartSetLineItemTotalPriceAction::class, $action));
459 1
        return $this;
460
    }
461
462
    /**
463
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-locale
464
     * @param CartSetLocaleAction|callable $action
465
     * @return $this
466
     */
467 1
    public function setLocale($action = null)
468
    {
469 1
        $this->addAction($this->resolveAction(CartSetLocaleAction::class, $action));
470 1
        return $this;
471
    }
472
473
    /**
474
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-shipping-address
475
     * @param CartSetShippingAddressAction|callable $action
476
     * @return $this
477
     */
478 1
    public function setShippingAddress($action = null)
479
    {
480 1
        $this->addAction($this->resolveAction(CartSetShippingAddressAction::class, $action));
481 1
        return $this;
482
    }
483
484
    /**
485
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-shippingmethod
486
     * @param CartSetShippingMethodAction|callable $action
487
     * @return $this
488
     */
489 1
    public function setShippingMethod($action = null)
490
    {
491 1
        $this->addAction($this->resolveAction(CartSetShippingMethodAction::class, $action));
492 1
        return $this;
493
    }
494
495
    /**
496
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-shippingmethod-taxamount
497
     * @param CartSetShippingMethodTaxAmountAction|callable $action
498
     * @return $this
499
     */
500 1
    public function setShippingMethodTaxAmount($action = null)
501
    {
502 1
        $this->addAction($this->resolveAction(CartSetShippingMethodTaxAmountAction::class, $action));
503 1
        return $this;
504
    }
505
506
    /**
507
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-shippingmethod-taxrate
508
     * @param CartSetShippingMethodTaxRateAction|callable $action
509
     * @return $this
510
     */
511 1
    public function setShippingMethodTaxRate($action = null)
512
    {
513 1
        $this->addAction($this->resolveAction(CartSetShippingMethodTaxRateAction::class, $action));
514 1
        return $this;
515
    }
516
517
    /**
518
     * @link https://dev.commercetools.com/http-api-projects-carts.html#set-shippingrateinput
519
     * @link https://docs.commercetools.com/http-api-projects-carts.html#set-shippingrateinput
520
     * @param CartSetShippingRateInputAction|callable $action
521
     * @return $this
522
     */
523 1
    public function setShippingRateInput($action = null)
524
    {
525 1
        $this->addAction($this->resolveAction(CartSetShippingRateInputAction::class, $action));
526 1
        return $this;
527
    }
528
529
    /**
530
     * @return CartsActionBuilder
531
     */
532
    public function of()
533
    {
534
        return new self();
535
    }
536
537
    /**
538
     * @param $class
539
     * @param $action
540
     * @return AbstractAction
541
     * @throws InvalidArgumentException
542
     */
543 43
    private function resolveAction($class, $action = null)
544
    {
545 43
        if (is_null($action) || is_callable($action)) {
546 43
            $callback = $action;
547 43
            $emptyAction = $class::of();
548 43
            $action = $this->callback($emptyAction, $callback);
549
        }
550 43
        if ($action instanceof $class) {
551 43
            return $action;
552
        }
553
        throw new InvalidArgumentException(
554
            sprintf('Expected method to be called with or callable to return %s', $class)
555
        );
556
    }
557
558
    /**
559
     * @param $action
560
     * @param callable $callback
561
     * @return AbstractAction
562
     */
563 43
    private function callback($action, callable $callback = null)
564
    {
565 43
        if (!is_null($callback)) {
566
            $action = $callback($action);
567
        }
568 43
        return $action;
569
    }
570
571
    /**
572
     * @param AbstractAction $action
573
     * @return $this;
574
     */
575 43
    public function addAction(AbstractAction $action)
576
    {
577 43
        $this->actions[] = $action;
578 43
        return $this;
579
    }
580
581
    /**
582
     * @return array
583
     */
584 43
    public function getActions()
585
    {
586 43
        return $this->actions;
587
    }
588
589
    /**
590
     * @param array $actions
591
     * @return $this
592
     */
593
    public function setActions(array $actions)
594
    {
595
        $this->actions = $actions;
596
        return $this;
597
    }
598
}
599