Test Failed
Push — develop ( a91eee...f77bbc )
by Jens
17:10 queued 15s
created

ShippingMethodsActionBuilder::setLocalizedName()   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\ShippingMethods\Command\ShippingMethodAddShippingRateAction;
8
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodAddZoneAction;
9
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodChangeIsDefaultAction;
10
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodChangeNameAction;
11
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodChangeTaxCategoryAction;
12
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodRemoveShippingRateAction;
13
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodRemoveZoneAction;
14
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodSetCustomFieldAction;
15
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodSetCustomTypeAction;
16
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodSetDescriptionAction;
17
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodSetKeyAction;
18
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodSetLocalizedDescriptionAction;
19
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodSetLocalizedNameAction;
20
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodSetPredicateAction;
21
22
class ShippingMethodsActionBuilder
23
{
24
    private $actions = [];
25
26
    /**
27
     * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#add-shippingrate
28 1
     * @param ShippingMethodAddShippingRateAction|callable $action
29
     * @return $this
30 1
     */
31 1
    public function addShippingRate($action = null)
32
    {
33
        $this->addAction($this->resolveAction(ShippingMethodAddShippingRateAction::class, $action));
34
        return $this;
35
    }
36
37
    /**
38
     * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#add-zone
39 1
     * @param ShippingMethodAddZoneAction|callable $action
40
     * @return $this
41 1
     */
42 1
    public function addZone($action = null)
43
    {
44
        $this->addAction($this->resolveAction(ShippingMethodAddZoneAction::class, $action));
45
        return $this;
46
    }
47
48
    /**
49
     * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#change-isdefault
50 1
     * @param ShippingMethodChangeIsDefaultAction|callable $action
51
     * @return $this
52 1
     */
53 1
    public function changeIsDefault($action = null)
54
    {
55
        $this->addAction($this->resolveAction(ShippingMethodChangeIsDefaultAction::class, $action));
56
        return $this;
57
    }
58
59
    /**
60
     * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#change-name
61 1
     * @param ShippingMethodChangeNameAction|callable $action
62
     * @return $this
63 1
     */
64 1
    public function changeName($action = null)
65
    {
66
        $this->addAction($this->resolveAction(ShippingMethodChangeNameAction::class, $action));
67
        return $this;
68
    }
69
70
    /**
71
     * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#change-taxcategory
72 1
     * @param ShippingMethodChangeTaxCategoryAction|callable $action
73
     * @return $this
74 1
     */
75 1
    public function changeTaxCategory($action = null)
76
    {
77
        $this->addAction($this->resolveAction(ShippingMethodChangeTaxCategoryAction::class, $action));
78
        return $this;
79
    }
80
81
    /**
82
     * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#remove-shippingrate
83 1
     * @param ShippingMethodRemoveShippingRateAction|callable $action
84
     * @return $this
85 1
     */
86 1
    public function removeShippingRate($action = null)
87
    {
88
        $this->addAction($this->resolveAction(ShippingMethodRemoveShippingRateAction::class, $action));
89
        return $this;
90
    }
91
92
    /**
93
     * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#remove-zone
94 1
     * @param ShippingMethodRemoveZoneAction|callable $action
95
     * @return $this
96 1
     */
97 1
    public function removeZone($action = null)
98
    {
99
        $this->addAction($this->resolveAction(ShippingMethodRemoveZoneAction::class, $action));
100
        return $this;
101
    }
102
103
    /**
104
     * @link https://docs.commercetools.com/api/projects/shippingMethods#set-customfield
105 1
     * @param ShippingMethodSetCustomFieldAction|callable $action
106
     * @return $this
107 1
     */
108 1
    public function setCustomField($action = null)
109
    {
110
        $this->addAction($this->resolveAction(ShippingMethodSetCustomFieldAction::class, $action));
111
        return $this;
112
    }
113
114
    /**
115
     * @link https://docs.commercetools.com/api/projects/shippingMethods#set-customtype
116 1
     * @param ShippingMethodSetCustomTypeAction|callable $action
117
     * @return $this
118 1
     */
119 1
    public function setCustomType($action = null)
120
    {
121
        $this->addAction($this->resolveAction(ShippingMethodSetCustomTypeAction::class, $action));
122
        return $this;
123
    }
124
125
    /**
126
     * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#set-description
127 1
     * @param ShippingMethodSetDescriptionAction|callable $action
128
     * @return $this
129 1
     */
130 1
    public function setDescription($action = null)
131
    {
132
        $this->addAction($this->resolveAction(ShippingMethodSetDescriptionAction::class, $action));
133
        return $this;
134
    }
135
136
    /**
137
     * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#set-key
138 1
     * @param ShippingMethodSetKeyAction|callable $action
139
     * @return $this
140 1
     */
141 1
    public function setKey($action = null)
142
    {
143
        $this->addAction($this->resolveAction(ShippingMethodSetKeyAction::class, $action));
144
        return $this;
145
    }
146
147
    /**
148
     * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#set-localized-description
149
     * @param ShippingMethodSetLocalizedDescriptionAction|callable $action
150
     * @return $this
151
     */
152
    public function setLocalizedDescription($action = null)
153
    {
154
        $this->addAction($this->resolveAction(ShippingMethodSetLocalizedDescriptionAction::class, $action));
155
        return $this;
156
    }
157
158 11
    /**
159
     * @link https://docs.commercetools.com/api/projects/shippingMethods#set-localized-name
160 11
     * @param ShippingMethodSetLocalizedNameAction|callable $action
161 11
     * @return $this
162 11
     */
163 11
    public function setLocalizedName($action = null)
164
    {
165 11
        $this->addAction($this->resolveAction(ShippingMethodSetLocalizedNameAction::class, $action));
166 11
        return $this;
167
    }
168
169
    /**
170
     * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#set-predicate
171
     * @param ShippingMethodSetPredicateAction|callable $action
172
     * @return $this
173
     */
174
    public function setPredicate($action = null)
175
    {
176
        $this->addAction($this->resolveAction(ShippingMethodSetPredicateAction::class, $action));
177
        return $this;
178 11
    }
179
180 11
    /**
181
     * @return ShippingMethodsActionBuilder
182
     */
183 11
    public static function of()
184
    {
185
        return new self();
186
    }
187
188
    /**
189
     * @param $class
190 11
     * @param $action
191
     * @return AbstractAction
192 11
     * @throws InvalidArgumentException
193 11
     */
194
    private function resolveAction($class, $action = null)
195
    {
196
        if (is_null($action) || is_callable($action)) {
197
            $callback = $action;
198
            $emptyAction = $class::of();
199 11
            $action = $this->callback($emptyAction, $callback);
200
        }
201 11
        if ($action instanceof $class) {
202
            return $action;
203
        }
204
        throw new InvalidArgumentException(
205
            sprintf('Expected method to be called with or callable to return %s', $class)
206
        );
207
    }
208
209
    /**
210
     * @param $action
211
     * @param callable $callback
212
     * @return AbstractAction
213
     */
214
    private function callback($action, callable $callback = null)
215
    {
216
        if (!is_null($callback)) {
217
            $action = $callback($action);
218
        }
219
        return $action;
220
    }
221
222
    /**
223
     * @param AbstractAction $action
224
     * @return $this;
225
     */
226
    public function addAction(AbstractAction $action)
227
    {
228
        $this->actions[] = $action;
229
        return $this;
230
    }
231
232
    /**
233
     * @return array
234
     */
235
    public function getActions()
236
    {
237
        return $this->actions;
238
    }
239
240
241
    /**
242
     * @param array $actions
243
     * @return $this
244
     */
245
    public function setActions(array $actions)
246
    {
247
        $this->actions = $actions;
248
        return $this;
249
    }
250
}
251