Passed
Push — develop ( b811fe...1e6961 )
by Jens
07:26 queued 12s
created

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