Passed
Push — master ( 6e2a06...da0795 )
by Jens
14:26
created

CartDiscountsActionBuilder::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
eloc 8
dl 0
loc 12
ccs 7
cts 9
cp 0.7778
rs 10
c 0
b 0
f 0
cc 4
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\CartDiscounts\Command\CartDiscountChangeCartPredicateAction;
8
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeIsActiveAction;
9
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeNameAction;
10
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeRequiresDiscountCodeAction;
11
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeSortOrderAction;
12
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeStackingModeAction;
13
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeTargetAction;
14
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeValueAction;
15
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetCustomFieldAction;
16
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetCustomTypeAction;
17
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetDescriptionAction;
18
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetValidFromAction;
19
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetValidFromAndUntilAction;
20
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetValidUntilAction;
21
22
class CartDiscountsActionBuilder
23
{
24
    private $actions = [];
25
26
    /**
27
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-cart-predicate
28
     * @param CartDiscountChangeCartPredicateAction|callable $action
29
     * @return $this
30
     */
31 1
    public function changeCartPredicate($action = null)
32
    {
33 1
        $this->addAction($this->resolveAction(CartDiscountChangeCartPredicateAction::class, $action));
34 1
        return $this;
35
    }
36
37
    /**
38
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-isactive
39
     * @param CartDiscountChangeIsActiveAction|callable $action
40
     * @return $this
41
     */
42 1
    public function changeIsActive($action = null)
43
    {
44 1
        $this->addAction($this->resolveAction(CartDiscountChangeIsActiveAction::class, $action));
45 1
        return $this;
46
    }
47
48
    /**
49
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-name
50
     * @param CartDiscountChangeNameAction|callable $action
51
     * @return $this
52
     */
53 1
    public function changeName($action = null)
54
    {
55 1
        $this->addAction($this->resolveAction(CartDiscountChangeNameAction::class, $action));
56 1
        return $this;
57
    }
58
59
    /**
60
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-requires-discountcode
61
     * @param CartDiscountChangeRequiresDiscountCodeAction|callable $action
62
     * @return $this
63
     */
64 1
    public function changeRequiresDiscountCode($action = null)
65
    {
66 1
        $this->addAction($this->resolveAction(CartDiscountChangeRequiresDiscountCodeAction::class, $action));
67 1
        return $this;
68
    }
69
70
    /**
71
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-sort-order
72
     * @param CartDiscountChangeSortOrderAction|callable $action
73
     * @return $this
74
     */
75 1
    public function changeSortOrder($action = null)
76
    {
77 1
        $this->addAction($this->resolveAction(CartDiscountChangeSortOrderAction::class, $action));
78 1
        return $this;
79
    }
80
81
    /**
82
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-stacking-mode
83
     * @param CartDiscountChangeStackingModeAction|callable $action
84
     * @return $this
85
     */
86 1
    public function changeStackingMode($action = null)
87
    {
88 1
        $this->addAction($this->resolveAction(CartDiscountChangeStackingModeAction::class, $action));
89 1
        return $this;
90
    }
91
92
    /**
93
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-target
94
     * @param CartDiscountChangeTargetAction|callable $action
95
     * @return $this
96
     */
97 1
    public function changeTarget($action = null)
98
    {
99 1
        $this->addAction($this->resolveAction(CartDiscountChangeTargetAction::class, $action));
100 1
        return $this;
101
    }
102
103
    /**
104
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-value
105
     * @param CartDiscountChangeValueAction|callable $action
106
     * @return $this
107
     */
108 1
    public function changeValue($action = null)
109
    {
110 1
        $this->addAction($this->resolveAction(CartDiscountChangeValueAction::class, $action));
111 1
        return $this;
112
    }
113
114
    /**
115
     *
116
     * @param CartDiscountSetCustomFieldAction|callable $action
117
     * @return $this
118
     */
119 1
    public function setCustomField($action = null)
120
    {
121 1
        $this->addAction($this->resolveAction(CartDiscountSetCustomFieldAction::class, $action));
122 1
        return $this;
123
    }
124
125
    /**
126
     *
127
     * @param CartDiscountSetCustomTypeAction|callable $action
128
     * @return $this
129
     */
130 1
    public function setCustomType($action = null)
131
    {
132 1
        $this->addAction($this->resolveAction(CartDiscountSetCustomTypeAction::class, $action));
133 1
        return $this;
134
    }
135
136
    /**
137
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#set-description
138
     * @param CartDiscountSetDescriptionAction|callable $action
139
     * @return $this
140
     */
141 1
    public function setDescription($action = null)
142
    {
143 1
        $this->addAction($this->resolveAction(CartDiscountSetDescriptionAction::class, $action));
144 1
        return $this;
145
    }
146
147
    /**
148
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#set-valid-from
149
     * @param CartDiscountSetValidFromAction|callable $action
150
     * @return $this
151
     */
152 1
    public function setValidFrom($action = null)
153
    {
154 1
        $this->addAction($this->resolveAction(CartDiscountSetValidFromAction::class, $action));
155 1
        return $this;
156
    }
157
158
    /**
159
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#set-valid-from-and-until
160
     * @param CartDiscountSetValidFromAndUntilAction|callable $action
161
     * @return $this
162
     */
163 1
    public function setValidFromAndUntil($action = null)
164
    {
165 1
        $this->addAction($this->resolveAction(CartDiscountSetValidFromAndUntilAction::class, $action));
166 1
        return $this;
167
    }
168
169
    /**
170
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#set-valid-until
171
     * @param CartDiscountSetValidUntilAction|callable $action
172
     * @return $this
173
     */
174 1
    public function setValidUntil($action = null)
175
    {
176 1
        $this->addAction($this->resolveAction(CartDiscountSetValidUntilAction::class, $action));
177 1
        return $this;
178
    }
179
180
    /**
181
     * @return CartDiscountsActionBuilder
182
     */
183
    public static function of()
184
    {
185
        return new self();
186
    }
187
188
    /**
189
     * @param $class
190
     * @param $action
191
     * @return AbstractAction
192
     * @throws InvalidArgumentException
193
     */
194 14
    private function resolveAction($class, $action = null)
195
    {
196 14
        if (is_null($action) || is_callable($action)) {
197 14
            $callback = $action;
198 14
            $emptyAction = $class::of();
199 14
            $action = $this->callback($emptyAction, $callback);
200
        }
201 14
        if ($action instanceof $class) {
202 14
            return $action;
203
        }
204
        throw new InvalidArgumentException(
205
            sprintf('Expected method to be called with or callable to return %s', $class)
206
        );
207
    }
208
209
    /**
210
     * @param $action
211
     * @param callable $callback
212
     * @return AbstractAction
213
     */
214 14
    private function callback($action, callable $callback = null)
215
    {
216 14
        if (!is_null($callback)) {
217
            $action = $callback($action);
218
        }
219 14
        return $action;
220
    }
221
222
    /**
223
     * @param AbstractAction $action
224
     * @return $this;
225
     */
226 14
    public function addAction(AbstractAction $action)
227
    {
228 14
        $this->actions[] = $action;
229 14
        return $this;
230
    }
231
232
    /**
233
     * @return array
234
     */
235 14
    public function getActions()
236
    {
237 14
        return $this->actions;
238
    }
239
240
241
    /**
242
     * @param array $actions
243
     * @return $this
244
     */
245
    public function setActions(array $actions)
246
    {
247
        $this->actions = $actions;
248
        return $this;
249
    }
250
}
251