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

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