ShoppingListsActionBuilder::setActions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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\ShoppingLists\Command\ShoppingListAddLineItemAction;
8
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListAddTextLineItemAction;
9
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListChangeLineItemQuantityAction;
10
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListChangeLineItemsOrderAction;
11
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListChangeNameAction;
12
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListChangeTextLineItemNameAction;
13
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListChangeTextLineItemQuantityAction;
14
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListChangeTextLineItemsOrderAction;
15
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListRemoveLineItemAction;
16
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListRemoveTextLineItemAction;
17
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetAnonymousIdAction;
18
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetCustomFieldAction;
19
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetCustomTypeAction;
20
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetCustomerAction;
21
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetDeleteDaysAfterLastModificationAction;
22
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetDescriptionAction;
23
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetKeyAction;
24
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetLineItemCustomFieldAction;
25
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetLineItemCustomTypeAction;
26
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetSlugAction;
27
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetStoreAction;
28
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetTextLineItemCustomFieldAction;
29
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetTextLineItemCustomTypeAction;
30
use Commercetools\Core\Request\ShoppingLists\Command\ShoppingListSetTextLineItemDescriptionAction;
31
32
class ShoppingListsActionBuilder
33
{
34
    private $actions = [];
35
36
    /**
37
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#add-lineitem
38
     * @param ShoppingListAddLineItemAction|callable $action
39
     * @return $this
40
     */
41 1
    public function addLineItem($action = null)
42
    {
43 1
        $this->addAction($this->resolveAction(ShoppingListAddLineItemAction::class, $action));
44 1
        return $this;
45
    }
46
47
    /**
48
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#add-textlineitem
49
     * @param ShoppingListAddTextLineItemAction|callable $action
50
     * @return $this
51
     */
52 1
    public function addTextLineItem($action = null)
53
    {
54 1
        $this->addAction($this->resolveAction(ShoppingListAddTextLineItemAction::class, $action));
55 1
        return $this;
56
    }
57
58
    /**
59
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#change-lineitem-quantity
60
     * @param ShoppingListChangeLineItemQuantityAction|callable $action
61
     * @return $this
62
     */
63 1
    public function changeLineItemQuantity($action = null)
64
    {
65 1
        $this->addAction($this->resolveAction(ShoppingListChangeLineItemQuantityAction::class, $action));
66 1
        return $this;
67
    }
68
69
    /**
70
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#change-lineitems-order
71
     * @param ShoppingListChangeLineItemsOrderAction|callable $action
72
     * @return $this
73
     */
74 1
    public function changeLineItemsOrder($action = null)
75
    {
76 1
        $this->addAction($this->resolveAction(ShoppingListChangeLineItemsOrderAction::class, $action));
77 1
        return $this;
78
    }
79
80
    /**
81
     * @link https://docs.commercetools.com/http-api-projects-types.html#change-name
82
     * @param ShoppingListChangeNameAction|callable $action
83
     * @return $this
84
     */
85 1
    public function changeName($action = null)
86
    {
87 1
        $this->addAction($this->resolveAction(ShoppingListChangeNameAction::class, $action));
88 1
        return $this;
89
    }
90
91
    /**
92
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#change-textlineitem-name
93
     * @param ShoppingListChangeTextLineItemNameAction|callable $action
94
     * @return $this
95
     */
96 1
    public function changeTextLineItemName($action = null)
97
    {
98 1
        $this->addAction($this->resolveAction(ShoppingListChangeTextLineItemNameAction::class, $action));
99 1
        return $this;
100
    }
101
102
    /**
103
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#change-textlineitem-quantity
104
     * @param ShoppingListChangeTextLineItemQuantityAction|callable $action
105
     * @return $this
106
     */
107 1
    public function changeTextLineItemQuantity($action = null)
108
    {
109 1
        $this->addAction($this->resolveAction(ShoppingListChangeTextLineItemQuantityAction::class, $action));
110 1
        return $this;
111
    }
112
113
    /**
114
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#change-textlineitems-order
115
     * @param ShoppingListChangeTextLineItemsOrderAction|callable $action
116
     * @return $this
117
     */
118 1
    public function changeTextLineItemsOrder($action = null)
119
    {
120 1
        $this->addAction($this->resolveAction(ShoppingListChangeTextLineItemsOrderAction::class, $action));
121 1
        return $this;
122
    }
123
124
    /**
125
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#remove-lineitem
126
     * @param ShoppingListRemoveLineItemAction|callable $action
127
     * @return $this
128
     */
129 1
    public function removeLineItem($action = null)
130
    {
131 1
        $this->addAction($this->resolveAction(ShoppingListRemoveLineItemAction::class, $action));
132 1
        return $this;
133
    }
134
135
    /**
136
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#remove-textlineitem
137
     * @param ShoppingListRemoveTextLineItemAction|callable $action
138
     * @return $this
139
     */
140 1
    public function removeTextLineItem($action = null)
141
    {
142 1
        $this->addAction($this->resolveAction(ShoppingListRemoveTextLineItemAction::class, $action));
143 1
        return $this;
144
    }
145
146
    /**
147
     *
148
     * @param ShoppingListSetAnonymousIdAction|callable $action
149
     * @return $this
150
     */
151 1
    public function setAnonymousId($action = null)
152
    {
153 1
        $this->addAction($this->resolveAction(ShoppingListSetAnonymousIdAction::class, $action));
154 1
        return $this;
155
    }
156
157
    /**
158
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-customField
159
     * @param ShoppingListSetCustomFieldAction|callable $action
160
     * @return $this
161
     */
162 1
    public function setCustomField($action = null)
163
    {
164 1
        $this->addAction($this->resolveAction(ShoppingListSetCustomFieldAction::class, $action));
165 1
        return $this;
166
    }
167
168
    /**
169
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-customType
170
     * @param ShoppingListSetCustomTypeAction|callable $action
171
     * @return $this
172
     */
173 1
    public function setCustomType($action = null)
174
    {
175 1
        $this->addAction($this->resolveAction(ShoppingListSetCustomTypeAction::class, $action));
176 1
        return $this;
177
    }
178
179
    /**
180
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-customer
181
     * @param ShoppingListSetCustomerAction|callable $action
182
     * @return $this
183
     */
184 1
    public function setCustomer($action = null)
185
    {
186 1
        $this->addAction($this->resolveAction(ShoppingListSetCustomerAction::class, $action));
187 1
        return $this;
188
    }
189
190
    /**
191
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-deletedaysafterlastmodification
192
     * @param ShoppingListSetDeleteDaysAfterLastModificationAction|callable $action
193
     * @return $this
194
     */
195 1
    public function setDeleteDaysAfterLastModification($action = null)
196
    {
197 1
        $this->addAction($this->resolveAction(ShoppingListSetDeleteDaysAfterLastModificationAction::class, $action));
198 1
        return $this;
199
    }
200
201
    /**
202
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#change-description
203
     * @param ShoppingListSetDescriptionAction|callable $action
204
     * @return $this
205
     */
206 1
    public function setDescription($action = null)
207
    {
208 1
        $this->addAction($this->resolveAction(ShoppingListSetDescriptionAction::class, $action));
209 1
        return $this;
210
    }
211
212
    /**
213
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-key
214
     * @param ShoppingListSetKeyAction|callable $action
215
     * @return $this
216
     */
217 1
    public function setKey($action = null)
218
    {
219 1
        $this->addAction($this->resolveAction(ShoppingListSetKeyAction::class, $action));
220 1
        return $this;
221
    }
222
223
    /**
224
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-lineitem-customfield
225
     * @param ShoppingListSetLineItemCustomFieldAction|callable $action
226
     * @return $this
227
     */
228 1
    public function setLineItemCustomField($action = null)
229
    {
230 1
        $this->addAction($this->resolveAction(ShoppingListSetLineItemCustomFieldAction::class, $action));
231 1
        return $this;
232
    }
233
234
    /**
235
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-lineitem-custom-type
236
     * @param ShoppingListSetLineItemCustomTypeAction|callable $action
237
     * @return $this
238
     */
239 1
    public function setLineItemCustomType($action = null)
240
    {
241 1
        $this->addAction($this->resolveAction(ShoppingListSetLineItemCustomTypeAction::class, $action));
242 1
        return $this;
243
    }
244
245
    /**
246
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-slug
247
     * @param ShoppingListSetSlugAction|callable $action
248
     * @return $this
249
     */
250 1
    public function setSlug($action = null)
251
    {
252 1
        $this->addAction($this->resolveAction(ShoppingListSetSlugAction::class, $action));
253 1
        return $this;
254
    }
255
256
    /**
257
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-store
258
     * @param ShoppingListSetStoreAction|callable $action
259
     * @return $this
260
     */
261 1
    public function setStore($action = null)
262
    {
263 1
        $this->addAction($this->resolveAction(ShoppingListSetStoreAction::class, $action));
264 1
        return $this;
265
    }
266
267
    /**
268
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-textlineitem-customfield
269
     * @param ShoppingListSetTextLineItemCustomFieldAction|callable $action
270
     * @return $this
271
     */
272 1
    public function setTextLineItemCustomField($action = null)
273
    {
274 1
        $this->addAction($this->resolveAction(ShoppingListSetTextLineItemCustomFieldAction::class, $action));
275 1
        return $this;
276
    }
277
278
    /**
279
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-textlineitem-custom-type
280
     * @param ShoppingListSetTextLineItemCustomTypeAction|callable $action
281
     * @return $this
282
     */
283 1
    public function setTextLineItemCustomType($action = null)
284
    {
285 1
        $this->addAction($this->resolveAction(ShoppingListSetTextLineItemCustomTypeAction::class, $action));
286 1
        return $this;
287
    }
288
289
    /**
290
     * @link https://docs.commercetools.com/http-api-projects-shoppingLists.html#set-textlineitem-description
291
     * @param ShoppingListSetTextLineItemDescriptionAction|callable $action
292
     * @return $this
293
     */
294 1
    public function setTextLineItemDescription($action = null)
295
    {
296 1
        $this->addAction($this->resolveAction(ShoppingListSetTextLineItemDescriptionAction::class, $action));
297 1
        return $this;
298
    }
299
300
    /**
301
     * @return ShoppingListsActionBuilder
302
     */
303
    public static function of()
304
    {
305
        return new self();
306
    }
307
308
    /**
309
     * @param $class
310
     * @param $action
311
     * @return AbstractAction
312
     * @throws InvalidArgumentException
313
     */
314 24
    private function resolveAction($class, $action = null)
315
    {
316 24
        if (is_null($action) || is_callable($action)) {
317 24
            $callback = $action;
318 24
            $emptyAction = $class::of();
319 24
            $action = $this->callback($emptyAction, $callback);
320
        }
321 24
        if ($action instanceof $class) {
322 24
            return $action;
323
        }
324
        throw new InvalidArgumentException(
325
            sprintf('Expected method to be called with or callable to return %s', $class)
326
        );
327
    }
328
329
    /**
330
     * @param $action
331
     * @param callable $callback
332
     * @return AbstractAction
333
     */
334 24
    private function callback($action, callable $callback = null)
335
    {
336 24
        if (!is_null($callback)) {
337
            $action = $callback($action);
338
        }
339 24
        return $action;
340
    }
341
342
    /**
343
     * @param AbstractAction $action
344
     * @return $this;
345
     */
346 24
    public function addAction(AbstractAction $action)
347
    {
348 24
        $this->actions[] = $action;
349 24
        return $this;
350
    }
351
352
    /**
353
     * @return array
354
     */
355 24
    public function getActions()
356
    {
357 24
        return $this->actions;
358
    }
359
360
361
    /**
362
     * @param array $actions
363
     * @return $this
364
     */
365
    public function setActions(array $actions)
366
    {
367
        $this->actions = $actions;
368
        return $this;
369
    }
370
}
371