Passed
Push — master ( 0d508a...6e2a06 )
by Jens
16:22 queued 10s
created

OrderEditsActionBuilder::setCustomField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
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\OrderEdits\Command\OrderEditAddStagedActionAction;
8
use Commercetools\Core\Request\OrderEdits\Command\OrderEditSetCommentAction;
9
use Commercetools\Core\Request\OrderEdits\Command\OrderEditSetCustomFieldAction;
10
use Commercetools\Core\Request\OrderEdits\Command\OrderEditSetCustomTypeAction;
11
use Commercetools\Core\Request\OrderEdits\Command\OrderEditSetKeyAction;
12
use Commercetools\Core\Request\OrderEdits\Command\OrderEditSetStagedActionsAction;
13
14
class OrderEditsActionBuilder
15
{
16
    private $actions = [];
17
18
    /**
19
     *
20
     * @param OrderEditAddStagedActionAction|callable $action
21
     * @return $this
22
     */
23 1
    public function addStagedAction($action = null)
24
    {
25 1
        $this->addAction($this->resolveAction(OrderEditAddStagedActionAction::class, $action));
26 1
        return $this;
27
    }
28
29
    /**
30
     *
31
     * @param OrderEditSetCommentAction|callable $action
32
     * @return $this
33
     */
34 1
    public function setComment($action = null)
35
    {
36 1
        $this->addAction($this->resolveAction(OrderEditSetCommentAction::class, $action));
37 1
        return $this;
38
    }
39
40
    /**
41
     *
42
     * @param OrderEditSetCustomFieldAction|callable $action
43
     * @return $this
44
     */
45 1
    public function setCustomField($action = null)
46
    {
47 1
        $this->addAction($this->resolveAction(OrderEditSetCustomFieldAction::class, $action));
48 1
        return $this;
49
    }
50
51
    /**
52
     *
53
     * @param OrderEditSetCustomTypeAction|callable $action
54
     * @return $this
55
     */
56 1
    public function setCustomType($action = null)
57
    {
58 1
        $this->addAction($this->resolveAction(OrderEditSetCustomTypeAction::class, $action));
59 1
        return $this;
60
    }
61
62
    /**
63
     *
64
     * @param OrderEditSetKeyAction|callable $action
65
     * @return $this
66
     */
67 1
    public function setKey($action = null)
68
    {
69 1
        $this->addAction($this->resolveAction(OrderEditSetKeyAction::class, $action));
70 1
        return $this;
71
    }
72
73
    /**
74
     *
75
     * @param OrderEditSetStagedActionsAction|callable $action
76
     * @return $this
77
     */
78 1
    public function setStagedActions($action = null)
79
    {
80 1
        $this->addAction($this->resolveAction(OrderEditSetStagedActionsAction::class, $action));
81 1
        return $this;
82
    }
83
84
    /**
85
     * @return OrderEditsActionBuilder
86
     */
87
    public static function of()
88
    {
89
        return new self();
90
    }
91
92
    /**
93
     * @param $class
94
     * @param $action
95
     * @return AbstractAction
96
     * @throws InvalidArgumentException
97
     */
98 6
    private function resolveAction($class, $action = null)
99
    {
100 6
        if (is_null($action) || is_callable($action)) {
101 6
            $callback = $action;
102 6
            $emptyAction = $class::of();
103 6
            $action = $this->callback($emptyAction, $callback);
104
        }
105 6
        if ($action instanceof $class) {
106 6
            return $action;
107
        }
108
        throw new InvalidArgumentException(
109
            sprintf('Expected method to be called with or callable to return %s', $class)
110
        );
111
    }
112
113
    /**
114
     * @param $action
115
     * @param callable $callback
116
     * @return AbstractAction
117
     */
118 6
    private function callback($action, callable $callback = null)
119
    {
120 6
        if (!is_null($callback)) {
121
            $action = $callback($action);
122
        }
123 6
        return $action;
124
    }
125
126
    /**
127
     * @param AbstractAction $action
128
     * @return $this;
129
     */
130 6
    public function addAction(AbstractAction $action)
131
    {
132 6
        $this->actions[] = $action;
133 6
        return $this;
134
    }
135
136
    /**
137
     * @return array
138
     */
139 6
    public function getActions()
140
    {
141 6
        return $this->actions;
142
    }
143
144
145
    /**
146
     * @param array $actions
147
     * @return $this
148
     */
149
    public function setActions(array $actions)
150
    {
151
        $this->actions = $actions;
152
        return $this;
153
    }
154
}
155