Passed
Push — develop ( bf4823...f9a3d1 )
by Jens
17:57 queued 15s
created

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