Passed
Push — develop ( f53d0b...3a55e6 )
by Jens
09:08
created

changeRequiresDiscountCode()   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\CartDiscountSetValidFromAction;
19
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetValidUntilAction;
20
21
class CartDiscountsActionBuilder
22
{
23
    private $actions = [];
24
25
    /**
26
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-cart-predicate
27
     * @param CartDiscountChangeCartPredicateAction|callable $action
28
     * @return $this
29
     */
30 1
    public function changeCartPredicate($action = null)
31
    {
32 1
        $this->addAction($this->resolveAction(CartDiscountChangeCartPredicateAction::class, $action));
33 1
        return $this;
34
    }
35
36
    /**
37
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-isactive
38
     * @param CartDiscountChangeIsActiveAction|callable $action
39
     * @return $this
40
     */
41 1
    public function changeIsActive($action = null)
42
    {
43 1
        $this->addAction($this->resolveAction(CartDiscountChangeIsActiveAction::class, $action));
44 1
        return $this;
45
    }
46
47
    /**
48
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-name
49
     * @param CartDiscountChangeNameAction|callable $action
50
     * @return $this
51
     */
52 1
    public function changeName($action = null)
53
    {
54 1
        $this->addAction($this->resolveAction(CartDiscountChangeNameAction::class, $action));
55 1
        return $this;
56
    }
57
58
    /**
59
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-requires-discountcode
60
     * @param CartDiscountChangeRequiresDiscountCodeAction|callable $action
61
     * @return $this
62
     */
63 1
    public function changeRequiresDiscountCode($action = null)
64
    {
65 1
        $this->addAction($this->resolveAction(CartDiscountChangeRequiresDiscountCodeAction::class, $action));
66 1
        return $this;
67
    }
68
69
    /**
70
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-sort-order
71
     * @param CartDiscountChangeSortOrderAction|callable $action
72
     * @return $this
73
     */
74 1
    public function changeSortOrder($action = null)
75
    {
76 1
        $this->addAction($this->resolveAction(CartDiscountChangeSortOrderAction::class, $action));
77 1
        return $this;
78
    }
79
80
    /**
81
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-stacking-mode
82
     * @param CartDiscountChangeStackingModeAction|callable $action
83
     * @return $this
84
     */
85 1
    public function changeStackingMode($action = null)
86
    {
87 1
        $this->addAction($this->resolveAction(CartDiscountChangeStackingModeAction::class, $action));
88 1
        return $this;
89
    }
90
91
    /**
92
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-target
93
     * @param CartDiscountChangeTargetAction|callable $action
94
     * @return $this
95
     */
96 1
    public function changeTarget($action = null)
97
    {
98 1
        $this->addAction($this->resolveAction(CartDiscountChangeTargetAction::class, $action));
99 1
        return $this;
100
    }
101
102
    /**
103
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#change-value
104
     * @param CartDiscountChangeValueAction|callable $action
105
     * @return $this
106
     */
107 1
    public function changeValue($action = null)
108
    {
109 1
        $this->addAction($this->resolveAction(CartDiscountChangeValueAction::class, $action));
110 1
        return $this;
111
    }
112
113
    /**
114
     *
115
     * @param CartDiscountSetCustomFieldAction|callable $action
116
     * @return $this
117
     */
118 1
    public function setCustomField($action = null)
119
    {
120 1
        $this->addAction($this->resolveAction(CartDiscountSetCustomFieldAction::class, $action));
121 1
        return $this;
122
    }
123
124
    /**
125
     *
126
     * @param CartDiscountSetCustomTypeAction|callable $action
127
     * @return $this
128
     */
129 1
    public function setCustomType($action = null)
130
    {
131 1
        $this->addAction($this->resolveAction(CartDiscountSetCustomTypeAction::class, $action));
132 1
        return $this;
133
    }
134
135
    /**
136
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#set-description
137
     * @param CartDiscountSetDescriptionAction|callable $action
138
     * @return $this
139
     */
140 1
    public function setDescription($action = null)
141
    {
142 1
        $this->addAction($this->resolveAction(CartDiscountSetDescriptionAction::class, $action));
143 1
        return $this;
144
    }
145
146
    /**
147
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#set-valid-from
148
     * @param CartDiscountSetValidFromAction|callable $action
149
     * @return $this
150
     */
151 1
    public function setValidFrom($action = null)
152
    {
153 1
        $this->addAction($this->resolveAction(CartDiscountSetValidFromAction::class, $action));
154 1
        return $this;
155
    }
156
157
    /**
158
     * @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#set-valid-until
159
     * @param CartDiscountSetValidUntilAction|callable $action
160
     * @return $this
161
     */
162 1
    public function setValidUntil($action = null)
163
    {
164 1
        $this->addAction($this->resolveAction(CartDiscountSetValidUntilAction::class, $action));
165 1
        return $this;
166
    }
167
168
    /**
169
     * @return CartDiscountsActionBuilder
170
     */
171
    public 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
     * @param array $actions
230
     * @return $this
231
     */
232
    public function setActions(array $actions)
233
    {
234
        $this->actions = $actions;
235
        return $this;
236
    }
237
}
238