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

InventoryActionBuilder   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Test Coverage

Coverage 82.98%

Importance

Changes 0
Metric Value
wmc 18
dl 0
loc 160
c 0
b 0
f 0
ccs 39
cts 47
cp 0.8298
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setExpectedDelivery() 0 4 1
A removeQuantity() 0 4 1
A callback() 0 6 2
A addAction() 0 4 1
A setCustomField() 0 4 1
A of() 0 3 1
A setRestockableInDays() 0 4 1
A setSupplyChannel() 0 4 1
A setCustomType() 0 4 1
A changeQuantity() 0 4 1
A resolveAction() 0 12 4
A addQuantity() 0 4 1
A setActions() 0 4 1
A getActions() 0 3 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\Inventory\Command\InventoryAddQuantityAction;
8
use Commercetools\Core\Request\Inventory\Command\InventoryChangeQuantityAction;
9
use Commercetools\Core\Request\Inventory\Command\InventoryRemoveQuantityAction;
10
use Commercetools\Core\Request\Inventory\Command\InventorySetCustomFieldAction;
11
use Commercetools\Core\Request\Inventory\Command\InventorySetCustomTypeAction;
12
use Commercetools\Core\Request\Inventory\Command\InventorySetExpectedDeliveryAction;
13
use Commercetools\Core\Request\Inventory\Command\InventorySetRestockableInDaysAction;
14
use Commercetools\Core\Request\Inventory\Command\InventorySetSupplyChannelAction;
15
16
class InventoryActionBuilder
17
{
18
    private $actions = [];
19
20
    /**
21
     * @link https://docs.commercetools.com/http-api-projects-inventory.html#add-quantity
22
     * @param InventoryAddQuantityAction|callable $action
23
     * @return $this
24
     */
25 1
    public function addQuantity($action = null)
26
    {
27 1
        $this->addAction($this->resolveAction(InventoryAddQuantityAction::class, $action));
28 1
        return $this;
29
    }
30
31
    /**
32
     * @link https://docs.commercetools.com/http-api-projects-inventory.html#change-quantity
33
     * @param InventoryChangeQuantityAction|callable $action
34
     * @return $this
35
     */
36 1
    public function changeQuantity($action = null)
37
    {
38 1
        $this->addAction($this->resolveAction(InventoryChangeQuantityAction::class, $action));
39 1
        return $this;
40
    }
41
42
    /**
43
     * @link https://docs.commercetools.com/http-api-projects-inventory.html#remove-quantity
44
     * @param InventoryRemoveQuantityAction|callable $action
45
     * @return $this
46
     */
47 1
    public function removeQuantity($action = null)
48
    {
49 1
        $this->addAction($this->resolveAction(InventoryRemoveQuantityAction::class, $action));
50 1
        return $this;
51
    }
52
53
    /**
54
     *
55
     * @param InventorySetCustomFieldAction|callable $action
56
     * @return $this
57
     */
58 1
    public function setCustomField($action = null)
59
    {
60 1
        $this->addAction($this->resolveAction(InventorySetCustomFieldAction::class, $action));
61 1
        return $this;
62
    }
63
64
    /**
65
     *
66
     * @param InventorySetCustomTypeAction|callable $action
67
     * @return $this
68
     */
69 1
    public function setCustomType($action = null)
70
    {
71 1
        $this->addAction($this->resolveAction(InventorySetCustomTypeAction::class, $action));
72 1
        return $this;
73
    }
74
75
    /**
76
     * @link https://docs.commercetools.com/http-api-projects-inventory.html#set-expecteddelivery
77
     * @param InventorySetExpectedDeliveryAction|callable $action
78
     * @return $this
79
     */
80 1
    public function setExpectedDelivery($action = null)
81
    {
82 1
        $this->addAction($this->resolveAction(InventorySetExpectedDeliveryAction::class, $action));
83 1
        return $this;
84
    }
85
86
    /**
87
     * @link https://docs.commercetools.com/http-api-projects-inventory.html#set-restockableindays
88
     * @param InventorySetRestockableInDaysAction|callable $action
89
     * @return $this
90
     */
91 1
    public function setRestockableInDays($action = null)
92
    {
93 1
        $this->addAction($this->resolveAction(InventorySetRestockableInDaysAction::class, $action));
94 1
        return $this;
95
    }
96
97
    /**
98
     * @link https://docs.commercetools.com/http-api-projects-inventory.html#set-supplychannel
99
     * @param InventorySetSupplyChannelAction|callable $action
100
     * @return $this
101
     */
102 1
    public function setSupplyChannel($action = null)
103
    {
104 1
        $this->addAction($this->resolveAction(InventorySetSupplyChannelAction::class, $action));
105 1
        return $this;
106
    }
107
108
    /**
109
     * @return InventoryActionBuilder
110
     */
111
    public function of()
112
    {
113
        return new self();
114
    }
115
116
    /**
117
     * @param $class
118
     * @param $action
119
     * @return AbstractAction
120
     * @throws InvalidArgumentException
121
     */
122 8
    private function resolveAction($class, $action = null)
123
    {
124 8
        if (is_null($action) || is_callable($action)) {
125 8
            $callback = $action;
126 8
            $emptyAction = $class::of();
127 8
            $action = $this->callback($emptyAction, $callback);
128
        }
129 8
        if ($action instanceof $class) {
130 8
            return $action;
131
        }
132
        throw new InvalidArgumentException(
133
            sprintf('Expected method to be called with or callable to return %s', $class)
134
        );
135
    }
136
137
    /**
138
     * @param $action
139
     * @param callable $callback
140
     * @return AbstractAction
141
     */
142 8
    private function callback($action, callable $callback = null)
143
    {
144 8
        if (!is_null($callback)) {
145
            $action = $callback($action);
146
        }
147 8
        return $action;
148
    }
149
150
    /**
151
     * @param AbstractAction $action
152
     * @return $this;
153
     */
154 8
    public function addAction(AbstractAction $action)
155
    {
156 8
        $this->actions[] = $action;
157 8
        return $this;
158
    }
159
160
    /**
161
     * @return array
162
     */
163 8
    public function getActions()
164
    {
165 8
        return $this->actions;
166
    }
167
168
    /**
169
     * @param array $actions
170
     * @return $this
171
     */
172
    public function setActions(array $actions)
173
    {
174
        $this->actions = $actions;
175
        return $this;
176
    }
177
}
178