Passed
Push — master ( 18b32f...fa2b73 )
by Jens
08:15
created

ProductDiscountsActionBuilder::setValidFrom()   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
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
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\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\ProductDiscountSetValidFromAction;
14
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetValidFromAndUntilAction;
15
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetValidUntilAction;
16
17
class ProductDiscountsActionBuilder
18
{
19
    private $actions = [];
20
21
    /**
22
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#change-is-active
23
     * @param ProductDiscountChangeIsActiveAction|callable $action
24
     * @return $this
25
     */
26 1
    public function changeIsActive($action = null)
27
    {
28 1
        $this->addAction($this->resolveAction(ProductDiscountChangeIsActiveAction::class, $action));
29 1
        return $this;
30
    }
31
32
    /**
33
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#change-name
34
     * @param ProductDiscountChangeNameAction|callable $action
35
     * @return $this
36
     */
37 1
    public function changeName($action = null)
38
    {
39 1
        $this->addAction($this->resolveAction(ProductDiscountChangeNameAction::class, $action));
40 1
        return $this;
41
    }
42
43
    /**
44
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#change-predicate
45
     * @param ProductDiscountChangePredicateAction|callable $action
46
     * @return $this
47
     */
48 1
    public function changePredicate($action = null)
49
    {
50 1
        $this->addAction($this->resolveAction(ProductDiscountChangePredicateAction::class, $action));
51 1
        return $this;
52
    }
53
54
    /**
55
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#change-sort-order
56
     * @param ProductDiscountChangeSortOrderAction|callable $action
57
     * @return $this
58
     */
59 1
    public function changeSortOrder($action = null)
60
    {
61 1
        $this->addAction($this->resolveAction(ProductDiscountChangeSortOrderAction::class, $action));
62 1
        return $this;
63
    }
64
65
    /**
66
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#change-value
67
     * @param ProductDiscountChangeValueAction|callable $action
68
     * @return $this
69
     */
70 1
    public function changeValue($action = null)
71
    {
72 1
        $this->addAction($this->resolveAction(ProductDiscountChangeValueAction::class, $action));
73 1
        return $this;
74
    }
75
76
    /**
77
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#set-description
78
     * @param ProductDiscountSetDescriptionAction|callable $action
79
     * @return $this
80
     */
81 1
    public function setDescription($action = null)
82
    {
83 1
        $this->addAction($this->resolveAction(ProductDiscountSetDescriptionAction::class, $action));
84 1
        return $this;
85
    }
86
87
    /**
88
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#set-valid-from
89
     * @param ProductDiscountSetValidFromAction|callable $action
90
     * @return $this
91
     */
92 1
    public function setValidFrom($action = null)
93
    {
94 1
        $this->addAction($this->resolveAction(ProductDiscountSetValidFromAction::class, $action));
95 1
        return $this;
96
    }
97
98
    /**
99
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#set-valid-from-and-until
100
     * @param ProductDiscountSetValidFromAndUntilAction|callable $action
101
     * @return $this
102
     */
103 1
    public function setValidFromAndUntil($action = null)
104
    {
105 1
        $this->addAction($this->resolveAction(ProductDiscountSetValidFromAndUntilAction::class, $action));
106 1
        return $this;
107
    }
108
109
    /**
110
     * @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#set-valid-until
111
     * @param ProductDiscountSetValidUntilAction|callable $action
112
     * @return $this
113
     */
114 1
    public function setValidUntil($action = null)
115
    {
116 1
        $this->addAction($this->resolveAction(ProductDiscountSetValidUntilAction::class, $action));
117 1
        return $this;
118
    }
119
120
    /**
121
     * @return ProductDiscountsActionBuilder
122
     */
123
    public function of()
124
    {
125
        return new self();
126
    }
127
128
    /**
129
     * @param $class
130
     * @param $action
131
     * @return AbstractAction
132
     * @throws InvalidArgumentException
133
     */
134 9
    private function resolveAction($class, $action = null)
135
    {
136 9
        if (is_null($action) || is_callable($action)) {
137 9
            $callback = $action;
138 9
            $emptyAction = $class::of();
139 9
            $action = $this->callback($emptyAction, $callback);
140
        }
141 9
        if ($action instanceof $class) {
142 9
            return $action;
143
        }
144
        throw new InvalidArgumentException(
145
            sprintf('Expected method to be called with or callable to return %s', $class)
146
        );
147
    }
148
149
    /**
150
     * @param $action
151
     * @param callable $callback
152
     * @return AbstractAction
153
     */
154 9
    private function callback($action, callable $callback = null)
155
    {
156 9
        if (!is_null($callback)) {
157
            $action = $callback($action);
158
        }
159 9
        return $action;
160
    }
161
162
    /**
163
     * @param AbstractAction $action
164
     * @return $this;
165
     */
166 9
    public function addAction(AbstractAction $action)
167
    {
168 9
        $this->actions[] = $action;
169 9
        return $this;
170
    }
171
172
    /**
173
     * @return array
174
     */
175 9
    public function getActions()
176
    {
177 9
        return $this->actions;
178
    }
179
180
    /**
181
     * @param array $actions
182
     * @return $this
183
     */
184
    public function setActions(array $actions)
185
    {
186
        $this->actions = $actions;
187
        return $this;
188
    }
189
}
190