Passed
Push — develop ( 14d0a2...ac4443 )
by Jens
08:35 queued 12s
created

ProductDiscountsActionBuilder::resolveAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
ccs 7
cts 9
cp 0.7778
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.1755
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\ProductDiscounts\Command\ProductDiscountChangeIsActiveAction;
8
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountChangeNameAction;
9
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountChangePredicateAction;
10
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountChangeSortOrderAction;
11
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountChangeValueAction;
12
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetDescriptionAction;
13
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetKeyAction;
14
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetValidFromAction;
15
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetValidFromAndUntilAction;
16
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetValidUntilAction;
17
18
class ProductDiscountsActionBuilder
19
{
20
    private $actions = [];
21
22
    /**
23
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#change-is-active
24
     * @param ProductDiscountChangeIsActiveAction|callable $action
25
     * @return $this
26
     */
27 1
    public function changeIsActive($action = null)
28
    {
29 1
        $this->addAction($this->resolveAction(ProductDiscountChangeIsActiveAction::class, $action));
30 1
        return $this;
31
    }
32
33
    /**
34
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#change-name
35
     * @param ProductDiscountChangeNameAction|callable $action
36
     * @return $this
37
     */
38 1
    public function changeName($action = null)
39
    {
40 1
        $this->addAction($this->resolveAction(ProductDiscountChangeNameAction::class, $action));
41 1
        return $this;
42
    }
43
44
    /**
45
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#change-predicate
46
     * @param ProductDiscountChangePredicateAction|callable $action
47
     * @return $this
48
     */
49 1
    public function changePredicate($action = null)
50
    {
51 1
        $this->addAction($this->resolveAction(ProductDiscountChangePredicateAction::class, $action));
52 1
        return $this;
53
    }
54
55
    /**
56
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#change-sort-order
57
     * @param ProductDiscountChangeSortOrderAction|callable $action
58
     * @return $this
59
     */
60 1
    public function changeSortOrder($action = null)
61
    {
62 1
        $this->addAction($this->resolveAction(ProductDiscountChangeSortOrderAction::class, $action));
63 1
        return $this;
64
    }
65
66
    /**
67
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#change-value
68
     * @param ProductDiscountChangeValueAction|callable $action
69
     * @return $this
70
     */
71 1
    public function changeValue($action = null)
72
    {
73 1
        $this->addAction($this->resolveAction(ProductDiscountChangeValueAction::class, $action));
74 1
        return $this;
75
    }
76
77
    /**
78
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#set-description
79
     * @param ProductDiscountSetDescriptionAction|callable $action
80
     * @return $this
81
     */
82 1
    public function setDescription($action = null)
83
    {
84 1
        $this->addAction($this->resolveAction(ProductDiscountSetDescriptionAction::class, $action));
85 1
        return $this;
86
    }
87
88
    /**
89
     *
90
     * @param ProductDiscountSetKeyAction|callable $action
91
     * @return $this
92
     */
93 1
    public function setKey($action = null)
94
    {
95 1
        $this->addAction($this->resolveAction(ProductDiscountSetKeyAction::class, $action));
96 1
        return $this;
97
    }
98
99
    /**
100
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#set-valid-from
101
     * @param ProductDiscountSetValidFromAction|callable $action
102
     * @return $this
103
     */
104 1
    public function setValidFrom($action = null)
105
    {
106 1
        $this->addAction($this->resolveAction(ProductDiscountSetValidFromAction::class, $action));
107 1
        return $this;
108
    }
109
110
    /**
111
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#set-valid-from-and-until
112
     * @param ProductDiscountSetValidFromAndUntilAction|callable $action
113
     * @return $this
114
     */
115 1
    public function setValidFromAndUntil($action = null)
116
    {
117 1
        $this->addAction($this->resolveAction(ProductDiscountSetValidFromAndUntilAction::class, $action));
118 1
        return $this;
119
    }
120
121
    /**
122
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#set-valid-until
123
     * @param ProductDiscountSetValidUntilAction|callable $action
124
     * @return $this
125
     */
126 1
    public function setValidUntil($action = null)
127
    {
128 1
        $this->addAction($this->resolveAction(ProductDiscountSetValidUntilAction::class, $action));
129 1
        return $this;
130
    }
131
132
    /**
133
     * @return ProductDiscountsActionBuilder
134
     */
135
    public static 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
    /**
194
     * @param array $actions
195
     * @return $this
196
     */
197
    public function setActions(array $actions)
198
    {
199
        $this->actions = $actions;
200
        return $this;
201
    }
202
}
203