Passed
Push — develop ( 96a913...1795ba )
by Jens
25:08 queued 14s
created

MeActionBuilder::getActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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\Me\Command\MyCartAddLineItemAction;
8
9
class MeActionBuilder
10
{
11
    private $actions = [];
12
13
    /**
14
     * @link https://docs.commercetools.com/api/projects/me-carts#add-lineitem
15
     * @param MyCartAddLineItemAction|callable $action
16
     * @return $this
17
     */
18
    public function addLineItem($action = null)
19
    {
20
        $this->addAction($this->resolveAction(MyCartAddLineItemAction::class, $action));
21
        return $this;
22
    }
23
24
    /**
25
     * @return MeActionBuilder
26
     */
27
    public static function of()
28
    {
29
        return new self();
30
    }
31
32
    /**
33
     * @param $class
34
     * @param $action
35
     * @return AbstractAction
36
     * @throws InvalidArgumentException
37
     */
38
    private function resolveAction($class, $action = null)
39
    {
40
        if (is_null($action) || is_callable($action)) {
41
            $callback = $action;
42
            $emptyAction = $class::of();
43
            $action = $this->callback($emptyAction, $callback);
44
        }
45
        if ($action instanceof $class) {
46
            return $action;
47
        }
48
        throw new InvalidArgumentException(
49
            sprintf('Expected method to be called with or callable to return %s', $class)
50
        );
51
    }
52
53
    /**
54
     * @param $action
55
     * @param callable $callback
56
     * @return AbstractAction
57
     */
58
    private function callback($action, callable $callback = null)
59
    {
60
        if (!is_null($callback)) {
61
            $action = $callback($action);
62
        }
63
        return $action;
64
    }
65
66
    /**
67
     * @param AbstractAction $action
68
     * @return $this;
69
     */
70
    public function addAction(AbstractAction $action)
71
    {
72
        $this->actions[] = $action;
73
        return $this;
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getActions()
80
    {
81
        return $this->actions;
82
    }
83
84
85
    /**
86
     * @param array $actions
87
     * @return $this
88
     */
89
    public function setActions(array $actions)
90
    {
91
        $this->actions = $actions;
92
        return $this;
93
    }
94
}
95