Passed
Pull Request — develop (#426)
by nikos
09:28
created

PaymentsActionBuilder::setAnonymousId()   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
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
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\Payments\Command\PaymentAddInterfaceInteractionAction;
8
use Commercetools\Core\Request\Payments\Command\PaymentAddTransactionAction;
9
use Commercetools\Core\Request\Payments\Command\PaymentChangeAmountPlannedAction;
10
use Commercetools\Core\Request\Payments\Command\PaymentChangeTransactionInteractionIdAction;
11
use Commercetools\Core\Request\Payments\Command\PaymentChangeTransactionStateAction;
12
use Commercetools\Core\Request\Payments\Command\PaymentChangeTransactionTimestampAction;
13
use Commercetools\Core\Request\Payments\Command\PaymentSetAmountPaidAction;
14
use Commercetools\Core\Request\Payments\Command\PaymentSetAmountRefundedAction;
15
use Commercetools\Core\Request\Payments\Command\PaymentSetAnonymousIdAction;
16
use Commercetools\Core\Request\Payments\Command\PaymentSetAuthorizationAction;
17
use Commercetools\Core\Request\Payments\Command\PaymentSetCustomFieldAction;
18
use Commercetools\Core\Request\Payments\Command\PaymentSetCustomTypeAction;
19
use Commercetools\Core\Request\Payments\Command\PaymentSetCustomerAction;
20
use Commercetools\Core\Request\Payments\Command\PaymentSetExternalIdAction;
21
use Commercetools\Core\Request\Payments\Command\PaymentSetInterfaceIdAction;
22
use Commercetools\Core\Request\Payments\Command\PaymentSetKeyAction;
23
use Commercetools\Core\Request\Payments\Command\PaymentSetMethodInfoInterfaceAction;
24
use Commercetools\Core\Request\Payments\Command\PaymentSetMethodInfoMethodAction;
25
use Commercetools\Core\Request\Payments\Command\PaymentSetMethodInfoNameAction;
26
use Commercetools\Core\Request\Payments\Command\PaymentSetStatusInterfaceCodeAction;
27
use Commercetools\Core\Request\Payments\Command\PaymentSetStatusInterfaceTextAction;
28
use Commercetools\Core\Request\Payments\Command\PaymentTransitionStateAction;
29
30
class PaymentsActionBuilder
31
{
32
    private $actions = [];
33
34
    /**
35
     * @link https://docs.commercetools.com/http-api-projects-payments.html#add-interfaceinteraction
36
     * @param PaymentAddInterfaceInteractionAction|callable $action
37
     * @return $this
38
     */
39 1
    public function addInterfaceInteraction($action = null)
40
    {
41 1
        $this->addAction($this->resolveAction(PaymentAddInterfaceInteractionAction::class, $action));
42 1
        return $this;
43
    }
44
45
    /**
46
     * @link https://docs.commercetools.com/http-api-projects-payments.html#add-transaction
47
     * @param PaymentAddTransactionAction|callable $action
48
     * @return $this
49
     */
50 1
    public function addTransaction($action = null)
51
    {
52 1
        $this->addAction($this->resolveAction(PaymentAddTransactionAction::class, $action));
53 1
        return $this;
54
    }
55
56
    /**
57
     * @link https://docs.commercetools.com/http-api-projects-payments.html#change-amountplanned
58
     * @param PaymentChangeAmountPlannedAction|callable $action
59
     * @return $this
60
     */
61 1
    public function changeAmountPlanned($action = null)
62
    {
63 1
        $this->addAction($this->resolveAction(PaymentChangeAmountPlannedAction::class, $action));
64 1
        return $this;
65
    }
66
67
    /**
68
     * @link https://docs.commercetools.com/http-api-projects-payments.html#change-transactioninteractionid
69
     * @param PaymentChangeTransactionInteractionIdAction|callable $action
70
     * @return $this
71
     */
72 1
    public function changeTransactionInteractionId($action = null)
73
    {
74 1
        $this->addAction($this->resolveAction(PaymentChangeTransactionInteractionIdAction::class, $action));
75 1
        return $this;
76
    }
77
78
    /**
79
     * @link https://docs.commercetools.com/http-api-projects-payments.html#change-transactionstate
80
     * @param PaymentChangeTransactionStateAction|callable $action
81
     * @return $this
82
     */
83 1
    public function changeTransactionState($action = null)
84
    {
85 1
        $this->addAction($this->resolveAction(PaymentChangeTransactionStateAction::class, $action));
86 1
        return $this;
87
    }
88
89
    /**
90
     * @link https://docs.commercetools.com/http-api-projects-payments.html#change-transactiontimestamp
91
     * @param PaymentChangeTransactionTimestampAction|callable $action
92
     * @return $this
93
     */
94 1
    public function changeTransactionTimestamp($action = null)
95
    {
96 1
        $this->addAction($this->resolveAction(PaymentChangeTransactionTimestampAction::class, $action));
97 1
        return $this;
98
    }
99
100
    /**
101
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-amountpaid
102
     * @param PaymentSetAmountPaidAction|callable $action
103
     * @return $this
104
     */
105 1
    public function setAmountPaid($action = null)
106
    {
107 1
        $this->addAction($this->resolveAction(PaymentSetAmountPaidAction::class, $action));
108 1
        return $this;
109
    }
110
111
    /**
112
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-amountrefunded
113
     * @param PaymentSetAmountRefundedAction|callable $action
114
     * @return $this
115
     */
116 1
    public function setAmountRefunded($action = null)
117
    {
118 1
        $this->addAction($this->resolveAction(PaymentSetAmountRefundedAction::class, $action));
119 1
        return $this;
120
    }
121
122
    /**
123
     * @link https://docs.commercetools.com/http-api-projects-payments#set-anonymousid
124
     * @param PaymentSetAnonymousIdAction|callable $action
125
     * @return $this
126
     */
127 1
    public function setAnonymousId($action = null)
128
    {
129 1
        $this->addAction($this->resolveAction(PaymentSetAnonymousIdAction::class, $action));
130 1
        return $this;
131
    }
132
133
    /**
134
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-authorization
135
     * @param PaymentSetAuthorizationAction|callable $action
136
     * @return $this
137
     */
138 1
    public function setAuthorization($action = null)
139
    {
140 1
        $this->addAction($this->resolveAction(PaymentSetAuthorizationAction::class, $action));
141 1
        return $this;
142
    }
143
144
    /**
145
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-customfield
146
     * @param PaymentSetCustomFieldAction|callable $action
147
     * @return $this
148
     */
149 1
    public function setCustomField($action = null)
150
    {
151 1
        $this->addAction($this->resolveAction(PaymentSetCustomFieldAction::class, $action));
152 1
        return $this;
153
    }
154
155
    /**
156
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-custom-type
157
     * @param PaymentSetCustomTypeAction|callable $action
158
     * @return $this
159
     */
160 1
    public function setCustomType($action = null)
161
    {
162 1
        $this->addAction($this->resolveAction(PaymentSetCustomTypeAction::class, $action));
163 1
        return $this;
164
    }
165
166
    /**
167
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-customer
168
     * @param PaymentSetCustomerAction|callable $action
169
     * @return $this
170
     */
171 1
    public function setCustomer($action = null)
172
    {
173 1
        $this->addAction($this->resolveAction(PaymentSetCustomerAction::class, $action));
174 1
        return $this;
175
    }
176
177
    /**
178
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-externalid
179
     * @param PaymentSetExternalIdAction|callable $action
180
     * @return $this
181
     */
182 1
    public function setExternalId($action = null)
183
    {
184 1
        $this->addAction($this->resolveAction(PaymentSetExternalIdAction::class, $action));
185 1
        return $this;
186
    }
187
188
    /**
189
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-interfaceid
190
     * @param PaymentSetInterfaceIdAction|callable $action
191
     * @return $this
192
     */
193 1
    public function setInterfaceId($action = null)
194
    {
195 1
        $this->addAction($this->resolveAction(PaymentSetInterfaceIdAction::class, $action));
196 1
        return $this;
197
    }
198
199
    /**
200
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-key
201
     * @param PaymentSetKeyAction|callable $action
202
     * @return $this
203
     */
204 1
    public function setKey($action = null)
205
    {
206 1
        $this->addAction($this->resolveAction(PaymentSetKeyAction::class, $action));
207 1
        return $this;
208
    }
209
210
    /**
211
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-methodinfointerface
212
     * @param PaymentSetMethodInfoInterfaceAction|callable $action
213
     * @return $this
214
     */
215 1
    public function setMethodInfoInterface($action = null)
216
    {
217 1
        $this->addAction($this->resolveAction(PaymentSetMethodInfoInterfaceAction::class, $action));
218 1
        return $this;
219
    }
220
221
    /**
222
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-methodinfomethod
223
     * @param PaymentSetMethodInfoMethodAction|callable $action
224
     * @return $this
225
     */
226 1
    public function setMethodInfoMethod($action = null)
227
    {
228 1
        $this->addAction($this->resolveAction(PaymentSetMethodInfoMethodAction::class, $action));
229 1
        return $this;
230
    }
231
232
    /**
233
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-methodinfoname
234
     * @param PaymentSetMethodInfoNameAction|callable $action
235
     * @return $this
236
     */
237 1
    public function setMethodInfoName($action = null)
238
    {
239 1
        $this->addAction($this->resolveAction(PaymentSetMethodInfoNameAction::class, $action));
240 1
        return $this;
241
    }
242
243
    /**
244
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-statusinterfacecode
245
     * @param PaymentSetStatusInterfaceCodeAction|callable $action
246
     * @return $this
247
     */
248 1
    public function setStatusInterfaceCode($action = null)
249
    {
250 1
        $this->addAction($this->resolveAction(PaymentSetStatusInterfaceCodeAction::class, $action));
251 1
        return $this;
252
    }
253
254
    /**
255
     * @link https://docs.commercetools.com/http-api-projects-payments.html#set-statusinterfacetext
256
     * @param PaymentSetStatusInterfaceTextAction|callable $action
257
     * @return $this
258
     */
259 1
    public function setStatusInterfaceText($action = null)
260
    {
261 1
        $this->addAction($this->resolveAction(PaymentSetStatusInterfaceTextAction::class, $action));
262 1
        return $this;
263
    }
264
265
    /**
266
     * @link https://docs.commercetools.com/http-api-projects-payments.html#transition-state
267
     * @param PaymentTransitionStateAction|callable $action
268
     * @return $this
269
     */
270 1
    public function transitionState($action = null)
271
    {
272 1
        $this->addAction($this->resolveAction(PaymentTransitionStateAction::class, $action));
273 1
        return $this;
274
    }
275
276
    /**
277
     * @return PaymentsActionBuilder
278
     */
279
    public function of()
280
    {
281
        return new self();
282
    }
283
284
    /**
285
     * @param $class
286
     * @param $action
287
     * @return AbstractAction
288
     * @throws InvalidArgumentException
289
     */
290 22
    private function resolveAction($class, $action = null)
291
    {
292 22
        if (is_null($action) || is_callable($action)) {
293 22
            $callback = $action;
294 22
            $emptyAction = $class::of();
295 22
            $action = $this->callback($emptyAction, $callback);
296
        }
297 22
        if ($action instanceof $class) {
298 22
            return $action;
299
        }
300
        throw new InvalidArgumentException(
301
            sprintf('Expected method to be called with or callable to return %s', $class)
302
        );
303
    }
304
305
    /**
306
     * @param $action
307
     * @param callable $callback
308
     * @return AbstractAction
309
     */
310 22
    private function callback($action, callable $callback = null)
311
    {
312 22
        if (!is_null($callback)) {
313
            $action = $callback($action);
314
        }
315 22
        return $action;
316
    }
317
318
    /**
319
     * @param AbstractAction $action
320
     * @return $this;
321
     */
322 22
    public function addAction(AbstractAction $action)
323
    {
324 22
        $this->actions[] = $action;
325 22
        return $this;
326
    }
327
328
    /**
329
     * @return array
330
     */
331 22
    public function getActions()
332
    {
333 22
        return $this->actions;
334
    }
335
336
    /**
337
     * @param array $actions
338
     * @return $this
339
     */
340
    public function setActions(array $actions)
341
    {
342
        $this->actions = $actions;
343
        return $this;
344
    }
345
}
346