Passed
Push — master ( 3f8301...c7321f )
by Jens
42:49 queued 17:59
created

CustomersActionBuilder::of()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 0
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\Customers\Command\CustomerAddAddressAction;
8
use Commercetools\Core\Request\Customers\Command\CustomerAddBillingAddressAction;
9
use Commercetools\Core\Request\Customers\Command\CustomerAddShippingAddressAction;
10
use Commercetools\Core\Request\Customers\Command\CustomerAddStoreAction;
11
use Commercetools\Core\Request\Customers\Command\CustomerChangeAddressAction;
12
use Commercetools\Core\Request\Customers\Command\CustomerChangeEmailAction;
13
use Commercetools\Core\Request\Customers\Command\CustomerRemoveAddressAction;
14
use Commercetools\Core\Request\Customers\Command\CustomerRemoveBillingAddressAction;
15
use Commercetools\Core\Request\Customers\Command\CustomerRemoveShippingAddressAction;
16
use Commercetools\Core\Request\Customers\Command\CustomerRemoveStoreAction;
17
use Commercetools\Core\Request\Customers\Command\CustomerSetCompanyNameAction;
18
use Commercetools\Core\Request\Customers\Command\CustomerSetCustomFieldAction;
19
use Commercetools\Core\Request\Customers\Command\CustomerSetCustomTypeAction;
20
use Commercetools\Core\Request\Customers\Command\CustomerSetCustomerGroupAction;
21
use Commercetools\Core\Request\Customers\Command\CustomerSetCustomerNumberAction;
22
use Commercetools\Core\Request\Customers\Command\CustomerSetDateOfBirthAction;
23
use Commercetools\Core\Request\Customers\Command\CustomerSetDefaultBillingAddressAction;
24
use Commercetools\Core\Request\Customers\Command\CustomerSetDefaultShippingAddressAction;
25
use Commercetools\Core\Request\Customers\Command\CustomerSetExternalIdAction;
26
use Commercetools\Core\Request\Customers\Command\CustomerSetFirstNameAction;
27
use Commercetools\Core\Request\Customers\Command\CustomerSetKeyAction;
28
use Commercetools\Core\Request\Customers\Command\CustomerSetLastNameAction;
29
use Commercetools\Core\Request\Customers\Command\CustomerSetLocaleAction;
30
use Commercetools\Core\Request\Customers\Command\CustomerSetMiddleNameAction;
31
use Commercetools\Core\Request\Customers\Command\CustomerSetSalutationAction;
32
use Commercetools\Core\Request\Customers\Command\CustomerSetStoresAction;
33
use Commercetools\Core\Request\Customers\Command\CustomerSetTitleAction;
34
use Commercetools\Core\Request\Customers\Command\CustomerSetVatIdAction;
35
36
class CustomersActionBuilder
37
{
38
    private $actions = [];
39
40
    /**
41
     * @link https://docs.commercetools.com/http-api-projects-customers.html#add-address
42
     * @param CustomerAddAddressAction|callable $action
43
     * @return $this
44
     */
45 1
    public function addAddress($action = null)
46
    {
47 1
        $this->addAction($this->resolveAction(CustomerAddAddressAction::class, $action));
48 1
        return $this;
49
    }
50
51
    /**
52
     * @link https://docs.commercetools.com/http-api-projects-customers.html#add-billing-address-id
53
     * @param CustomerAddBillingAddressAction|callable $action
54
     * @return $this
55
     */
56 1
    public function addBillingAddressId($action = null)
57
    {
58 1
        $this->addAction($this->resolveAction(CustomerAddBillingAddressAction::class, $action));
59 1
        return $this;
60
    }
61
62
    /**
63
     * @link https://docs.commercetools.com/http-api-projects-customers.html#add-billing-address-id
64
     * @param CustomerAddShippingAddressAction|callable $action
65
     * @return $this
66
     */
67 1
    public function addShippingAddressId($action = null)
68
    {
69 1
        $this->addAction($this->resolveAction(CustomerAddShippingAddressAction::class, $action));
70 1
        return $this;
71
    }
72
73
    /**
74
     * @link https://docs.commercetools.com/http-api-projects-customers.html#add-store-beta
75
     * @param CustomerAddStoreAction|callable $action
76
     * @return $this
77
     */
78 1
    public function addStore($action = null)
79
    {
80 1
        $this->addAction($this->resolveAction(CustomerAddStoreAction::class, $action));
81 1
        return $this;
82
    }
83
84
    /**
85
     * @link https://docs.commercetools.com/http-api-projects-customers.html#change-address
86
     * @param CustomerChangeAddressAction|callable $action
87
     * @return $this
88
     */
89 1
    public function changeAddress($action = null)
90
    {
91 1
        $this->addAction($this->resolveAction(CustomerChangeAddressAction::class, $action));
92 1
        return $this;
93
    }
94
95
    /**
96
     * @link https://docs.commercetools.com/http-api-projects-customers.html#change-email
97
     * @param CustomerChangeEmailAction|callable $action
98
     * @return $this
99
     */
100 1
    public function changeEmail($action = null)
101
    {
102 1
        $this->addAction($this->resolveAction(CustomerChangeEmailAction::class, $action));
103 1
        return $this;
104
    }
105
106
    /**
107
     * @link https://docs.commercetools.com/http-api-projects-customers.html#remove-address
108
     * @param CustomerRemoveAddressAction|callable $action
109
     * @return $this
110
     */
111 1
    public function removeAddress($action = null)
112
    {
113 1
        $this->addAction($this->resolveAction(CustomerRemoveAddressAction::class, $action));
114 1
        return $this;
115
    }
116
117
    /**
118
     * @link https://docs.commercetools.com/http-api-projects-customers.html#add-billing-address-id
119
     * @param CustomerRemoveBillingAddressAction|callable $action
120
     * @return $this
121
     */
122 1
    public function removeBillingAddressId($action = null)
123
    {
124 1
        $this->addAction($this->resolveAction(CustomerRemoveBillingAddressAction::class, $action));
125 1
        return $this;
126
    }
127
128
    /**
129
     * @link https://docs.commercetools.com/http-api-projects-customers.html#add-billing-address-id
130
     * @param CustomerRemoveShippingAddressAction|callable $action
131
     * @return $this
132
     */
133 1
    public function removeShippingAddressId($action = null)
134
    {
135 1
        $this->addAction($this->resolveAction(CustomerRemoveShippingAddressAction::class, $action));
136 1
        return $this;
137
    }
138
139
    /**
140
     * @link https://docs.commercetools.com/http-api-projects-customers.html#remove-store-beta
141
     * @param CustomerRemoveStoreAction|callable $action
142
     * @return $this
143
     */
144 1
    public function removeStore($action = null)
145
    {
146 1
        $this->addAction($this->resolveAction(CustomerRemoveStoreAction::class, $action));
147 1
        return $this;
148
    }
149
150
    /**
151
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-company-name
152
     * @param CustomerSetCompanyNameAction|callable $action
153
     * @return $this
154
     */
155 1
    public function setCompanyName($action = null)
156
    {
157 1
        $this->addAction($this->resolveAction(CustomerSetCompanyNameAction::class, $action));
158 1
        return $this;
159
    }
160
161
    /**
162
     *
163
     * @param CustomerSetCustomFieldAction|callable $action
164
     * @return $this
165
     */
166 1
    public function setCustomField($action = null)
167
    {
168 1
        $this->addAction($this->resolveAction(CustomerSetCustomFieldAction::class, $action));
169 1
        return $this;
170
    }
171
172
    /**
173
     *
174
     * @param CustomerSetCustomTypeAction|callable $action
175
     * @return $this
176
     */
177 1
    public function setCustomType($action = null)
178
    {
179 1
        $this->addAction($this->resolveAction(CustomerSetCustomTypeAction::class, $action));
180 1
        return $this;
181
    }
182
183
    /**
184
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-customergroup
185
     * @param CustomerSetCustomerGroupAction|callable $action
186
     * @return $this
187
     */
188 1
    public function setCustomerGroup($action = null)
189
    {
190 1
        $this->addAction($this->resolveAction(CustomerSetCustomerGroupAction::class, $action));
191 1
        return $this;
192
    }
193
194
    /**
195
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-customer-number
196
     * @param CustomerSetCustomerNumberAction|callable $action
197
     * @return $this
198
     */
199 1
    public function setCustomerNumber($action = null)
200
    {
201 1
        $this->addAction($this->resolveAction(CustomerSetCustomerNumberAction::class, $action));
202 1
        return $this;
203
    }
204
205
    /**
206
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-date-of-birth
207
     * @param CustomerSetDateOfBirthAction|callable $action
208
     * @return $this
209
     */
210 1
    public function setDateOfBirth($action = null)
211
    {
212 1
        $this->addAction($this->resolveAction(CustomerSetDateOfBirthAction::class, $action));
213 1
        return $this;
214
    }
215
216
    /**
217
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-default-billing-address
218
     * @param CustomerSetDefaultBillingAddressAction|callable $action
219
     * @return $this
220
     */
221 1
    public function setDefaultBillingAddress($action = null)
222
    {
223 1
        $this->addAction($this->resolveAction(CustomerSetDefaultBillingAddressAction::class, $action));
224 1
        return $this;
225
    }
226
227
    /**
228
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-default-shipping-address
229
     * @param CustomerSetDefaultShippingAddressAction|callable $action
230
     * @return $this
231
     */
232 1
    public function setDefaultShippingAddress($action = null)
233
    {
234 1
        $this->addAction($this->resolveAction(CustomerSetDefaultShippingAddressAction::class, $action));
235 1
        return $this;
236
    }
237
238
    /**
239
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-external-id
240
     * @param CustomerSetExternalIdAction|callable $action
241
     * @return $this
242
     */
243 1
    public function setExternalId($action = null)
244
    {
245 1
        $this->addAction($this->resolveAction(CustomerSetExternalIdAction::class, $action));
246 1
        return $this;
247
    }
248
249
    /**
250
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-first-name
251
     * @param CustomerSetFirstNameAction|callable $action
252
     * @return $this
253
     */
254 1
    public function setFirstName($action = null)
255
    {
256 1
        $this->addAction($this->resolveAction(CustomerSetFirstNameAction::class, $action));
257 1
        return $this;
258
    }
259
260
    /**
261
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-key
262
     * @param CustomerSetKeyAction|callable $action
263
     * @return $this
264
     */
265 1
    public function setKey($action = null)
266
    {
267 1
        $this->addAction($this->resolveAction(CustomerSetKeyAction::class, $action));
268 1
        return $this;
269
    }
270
271
    /**
272
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-last-name
273
     * @param CustomerSetLastNameAction|callable $action
274
     * @return $this
275
     */
276 1
    public function setLastName($action = null)
277
    {
278 1
        $this->addAction($this->resolveAction(CustomerSetLastNameAction::class, $action));
279 1
        return $this;
280
    }
281
282
    /**
283
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-locale
284
     * @param CustomerSetLocaleAction|callable $action
285
     * @return $this
286
     */
287 1
    public function setLocale($action = null)
288
    {
289 1
        $this->addAction($this->resolveAction(CustomerSetLocaleAction::class, $action));
290 1
        return $this;
291
    }
292
293
    /**
294
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-middle-name
295
     * @param CustomerSetMiddleNameAction|callable $action
296
     * @return $this
297
     */
298 1
    public function setMiddleName($action = null)
299
    {
300 1
        $this->addAction($this->resolveAction(CustomerSetMiddleNameAction::class, $action));
301 1
        return $this;
302
    }
303
304
    /**
305
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-salutation
306
     * @param CustomerSetSalutationAction|callable $action
307
     * @return $this
308
     */
309 1
    public function setSalutation($action = null)
310
    {
311 1
        $this->addAction($this->resolveAction(CustomerSetSalutationAction::class, $action));
312 1
        return $this;
313
    }
314
315
    /**
316
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-stores-beta
317
     * @param CustomerSetStoresAction|callable $action
318
     * @return $this
319
     */
320 1
    public function setStores($action = null)
321
    {
322 1
        $this->addAction($this->resolveAction(CustomerSetStoresAction::class, $action));
323 1
        return $this;
324
    }
325
326
    /**
327
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-title
328
     * @param CustomerSetTitleAction|callable $action
329
     * @return $this
330
     */
331 1
    public function setTitle($action = null)
332
    {
333 1
        $this->addAction($this->resolveAction(CustomerSetTitleAction::class, $action));
334 1
        return $this;
335
    }
336
337
    /**
338
     * @link https://docs.commercetools.com/http-api-projects-customers.html#set-vat-id
339
     * @param CustomerSetVatIdAction|callable $action
340
     * @return $this
341
     */
342 1
    public function setVatId($action = null)
343
    {
344 1
        $this->addAction($this->resolveAction(CustomerSetVatIdAction::class, $action));
345 1
        return $this;
346
    }
347
348
    /**
349
     * @return CustomersActionBuilder
350
     */
351
    public static function of()
352
    {
353
        return new self();
354
    }
355
356
    /**
357
     * @param $class
358
     * @param $action
359
     * @return AbstractAction
360
     * @throws InvalidArgumentException
361
     */
362 28
    private function resolveAction($class, $action = null)
363
    {
364 28
        if (is_null($action) || is_callable($action)) {
365 28
            $callback = $action;
366 28
            $emptyAction = $class::of();
367 28
            $action = $this->callback($emptyAction, $callback);
368
        }
369 28
        if ($action instanceof $class) {
370 28
            return $action;
371
        }
372
        throw new InvalidArgumentException(
373
            sprintf('Expected method to be called with or callable to return %s', $class)
374
        );
375
    }
376
377
    /**
378
     * @param $action
379
     * @param callable $callback
380
     * @return AbstractAction
381
     */
382 28
    private function callback($action, callable $callback = null)
383
    {
384 28
        if (!is_null($callback)) {
385
            $action = $callback($action);
386
        }
387 28
        return $action;
388
    }
389
390
    /**
391
     * @param AbstractAction $action
392
     * @return $this;
393
     */
394 28
    public function addAction(AbstractAction $action)
395
    {
396 28
        $this->actions[] = $action;
397 28
        return $this;
398
    }
399
400
    /**
401
     * @return array
402
     */
403 28
    public function getActions()
404
    {
405 28
        return $this->actions;
406
    }
407
408
409
    /**
410
     * @param array $actions
411
     * @return $this
412
     */
413
    public function setActions(array $actions)
414
    {
415
        $this->actions = $actions;
416
        return $this;
417
    }
418
}
419