Test Failed
Push — develop ( 99d0ad...a91eee )
by Jens
47:28 queued 25:36
created

ShippingMethodsActionBuilder::setCustomType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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