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

ShippingMethodsActionBuilder::getActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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