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

DiscountCodesActionBuilder::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\DiscountCodes\Command\DiscountCodeChangeCartDiscountsAction;
8
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeChangeGroupsAction;
9
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeChangeIsActiveAction;
10
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetCartPredicateAction;
11
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetCustomFieldAction;
12
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetCustomTypeAction;
13
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetDescriptionAction;
14
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetMaxApplicationsAction;
15
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetMaxApplicationsPerCustomerAction;
16
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetNameAction;
17
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetValidFromAction;
18
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetValidFromAndUntilAction;
19
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetValidUntilAction;
20
21
class DiscountCodesActionBuilder
22
{
23
    private $actions = [];
24
25
    /**
26
     * @link https://docs.commercetools.com/http-api-projects-discountCodes.html#change-cartdiscounts
27
     * @param DiscountCodeChangeCartDiscountsAction|callable $action
28
     * @return $this
29
     */
30 1
    public function changeCartDiscounts($action = null)
31
    {
32 1
        $this->addAction($this->resolveAction(DiscountCodeChangeCartDiscountsAction::class, $action));
33 1
        return $this;
34
    }
35
36
    /**
37
     * @link https://docs.commercetools.com/http-api-projects-discountCodes.html#change-groups
38
     * @param DiscountCodeChangeGroupsAction|callable $action
39
     * @return $this
40
     */
41 1
    public function changeGroups($action = null)
42
    {
43 1
        $this->addAction($this->resolveAction(DiscountCodeChangeGroupsAction::class, $action));
44 1
        return $this;
45
    }
46
47
    /**
48
     * @link https://docs.commercetools.com/http-api-projects-discountCodes.html#change-isactive
49
     * @param DiscountCodeChangeIsActiveAction|callable $action
50
     * @return $this
51
     */
52 1
    public function changeIsActive($action = null)
53
    {
54 1
        $this->addAction($this->resolveAction(DiscountCodeChangeIsActiveAction::class, $action));
55 1
        return $this;
56
    }
57
58
    /**
59
     * @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-cart-predicate
60
     * @param DiscountCodeSetCartPredicateAction|callable $action
61
     * @return $this
62
     */
63 1
    public function setCartPredicate($action = null)
64
    {
65 1
        $this->addAction($this->resolveAction(DiscountCodeSetCartPredicateAction::class, $action));
66 1
        return $this;
67
    }
68
69
    /**
70
     *
71
     * @param DiscountCodeSetCustomFieldAction|callable $action
72
     * @return $this
73
     */
74 1
    public function setCustomField($action = null)
75
    {
76 1
        $this->addAction($this->resolveAction(DiscountCodeSetCustomFieldAction::class, $action));
77 1
        return $this;
78
    }
79
80
    /**
81
     *
82
     * @param DiscountCodeSetCustomTypeAction|callable $action
83
     * @return $this
84
     */
85 1
    public function setCustomType($action = null)
86
    {
87 1
        $this->addAction($this->resolveAction(DiscountCodeSetCustomTypeAction::class, $action));
88 1
        return $this;
89
    }
90
91
    /**
92
     * @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-description
93
     * @param DiscountCodeSetDescriptionAction|callable $action
94
     * @return $this
95
     */
96 1
    public function setDescription($action = null)
97
    {
98 1
        $this->addAction($this->resolveAction(DiscountCodeSetDescriptionAction::class, $action));
99 1
        return $this;
100
    }
101
102
    /**
103
     * @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-max-applications
104
     * @param DiscountCodeSetMaxApplicationsAction|callable $action
105
     * @return $this
106
     */
107 1
    public function setMaxApplications($action = null)
108
    {
109 1
        $this->addAction($this->resolveAction(DiscountCodeSetMaxApplicationsAction::class, $action));
110 1
        return $this;
111
    }
112
113
    /**
114
     * @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-max-applications-per-customer
115
     * @param DiscountCodeSetMaxApplicationsPerCustomerAction|callable $action
116
     * @return $this
117
     */
118 1
    public function setMaxApplicationsPerCustomer($action = null)
119
    {
120 1
        $this->addAction($this->resolveAction(DiscountCodeSetMaxApplicationsPerCustomerAction::class, $action));
121 1
        return $this;
122
    }
123
124
    /**
125
     * @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-name
126
     * @param DiscountCodeSetNameAction|callable $action
127
     * @return $this
128
     */
129 1
    public function setName($action = null)
130
    {
131 1
        $this->addAction($this->resolveAction(DiscountCodeSetNameAction::class, $action));
132 1
        return $this;
133
    }
134
135
    /**
136
     * @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-valid-from
137
     * @param DiscountCodeSetValidFromAction|callable $action
138
     * @return $this
139
     */
140 1
    public function setValidFrom($action = null)
141
    {
142 1
        $this->addAction($this->resolveAction(DiscountCodeSetValidFromAction::class, $action));
143 1
        return $this;
144
    }
145
146
    /**
147
     * @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-valid-from-and-until
148
     * @param DiscountCodeSetValidFromAndUntilAction|callable $action
149
     * @return $this
150
     */
151 1
    public function setValidFromAndUntil($action = null)
152
    {
153 1
        $this->addAction($this->resolveAction(DiscountCodeSetValidFromAndUntilAction::class, $action));
154 1
        return $this;
155
    }
156
157
    /**
158
     * @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-valid-until
159
     * @param DiscountCodeSetValidUntilAction|callable $action
160
     * @return $this
161
     */
162 1
    public function setValidUntil($action = null)
163
    {
164 1
        $this->addAction($this->resolveAction(DiscountCodeSetValidUntilAction::class, $action));
165 1
        return $this;
166
    }
167
168
    /**
169
     * @return DiscountCodesActionBuilder
170
     */
171
    public static function of()
172
    {
173
        return new self();
174
    }
175
176
    /**
177
     * @param $class
178
     * @param $action
179
     * @return AbstractAction
180
     * @throws InvalidArgumentException
181
     */
182 13
    private function resolveAction($class, $action = null)
183
    {
184 13
        if (is_null($action) || is_callable($action)) {
185 13
            $callback = $action;
186 13
            $emptyAction = $class::of();
187 13
            $action = $this->callback($emptyAction, $callback);
188
        }
189 13
        if ($action instanceof $class) {
190 13
            return $action;
191
        }
192
        throw new InvalidArgumentException(
193
            sprintf('Expected method to be called with or callable to return %s', $class)
194
        );
195
    }
196
197
    /**
198
     * @param $action
199
     * @param callable $callback
200
     * @return AbstractAction
201
     */
202 13
    private function callback($action, callable $callback = null)
203
    {
204 13
        if (!is_null($callback)) {
205
            $action = $callback($action);
206
        }
207 13
        return $action;
208
    }
209
210
    /**
211
     * @param AbstractAction $action
212
     * @return $this;
213
     */
214 13
    public function addAction(AbstractAction $action)
215
    {
216 13
        $this->actions[] = $action;
217 13
        return $this;
218
    }
219
220
    /**
221
     * @return array
222
     */
223 13
    public function getActions()
224
    {
225 13
        return $this->actions;
226
    }
227
228
229
    /**
230
     * @param array $actions
231
     * @return $this
232
     */
233
    public function setActions(array $actions)
234
    {
235
        $this->actions = $actions;
236
        return $this;
237
    }
238
}
239