Passed
Push — master ( 94e9fa...54c66f )
by Barbara
84:38 queued 23:15
created

StoresActionBuilder::removeDistributionChannel()   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
eloc 2
c 0
b 0
f 0
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\Stores\Command\StoreAddDistributionChannelAction;
8
use Commercetools\Core\Request\Stores\Command\StoreAddSupplyChannelAction;
9
use Commercetools\Core\Request\Stores\Command\StoreRemoveDistributionChannelAction;
10
use Commercetools\Core\Request\Stores\Command\StoreRemoveSupplyChannelAction;
11
use Commercetools\Core\Request\Stores\Command\StoreSetDistributionChannelsAction;
12
use Commercetools\Core\Request\Stores\Command\StoreSetLanguagesAction;
13
use Commercetools\Core\Request\Stores\Command\StoreSetNameAction;
14
use Commercetools\Core\Request\Stores\Command\StoreSetSupplyChannelsAction;
15
16
class StoresActionBuilder
17
{
18
    private $actions = [];
19
20
    /**
21
     * @link https://docs.commercetools.com/api/projects/stores#add-distribution-channel
22
     * @param StoreAddDistributionChannelAction|callable $action
23
     * @return $this
24
     */
25 1
    public function addDistributionChannel($action = null)
26
    {
27 1
        $this->addAction($this->resolveAction(StoreAddDistributionChannelAction::class, $action));
28 1
        return $this;
29
    }
30
31
    /**
32
     * @link https://docs.commercetools.com/api/projects/stores#add-supply-channel
33
     * @param StoreAddSupplyChannelAction|callable $action
34
     * @return $this
35
     */
36 1
    public function addSupplyChannel($action = null)
37
    {
38 1
        $this->addAction($this->resolveAction(StoreAddSupplyChannelAction::class, $action));
39 1
        return $this;
40
    }
41
42
    /**
43
     * @link https://docs.commercetools.com/api/projects/stores#remove-distribution-channel
44
     * @param StoreRemoveDistributionChannelAction|callable $action
45
     * @return $this
46
     */
47 1
    public function removeDistributionChannel($action = null)
48
    {
49 1
        $this->addAction($this->resolveAction(StoreRemoveDistributionChannelAction::class, $action));
50 1
        return $this;
51
    }
52
53
    /**
54
     * @link https://docs.commercetools.com/api/projects/stores#remove-supply-channel
55
     * @param StoreRemoveSupplyChannelAction|callable $action
56
     * @return $this
57
     */
58 1
    public function removeSupplyChannel($action = null)
59
    {
60 1
        $this->addAction($this->resolveAction(StoreRemoveSupplyChannelAction::class, $action));
61 1
        return $this;
62
    }
63
64
    /**
65
     * @link https://docs.commercetools.com/api/projects/stores#set-distribution-channels
66
     * @param StoreSetDistributionChannelsAction|callable $action
67
     * @return $this
68
     */
69 1
    public function setDistributionChannels($action = null)
70
    {
71 1
        $this->addAction($this->resolveAction(StoreSetDistributionChannelsAction::class, $action));
72 1
        return $this;
73
    }
74
75
    /**
76
     * @link https://docs.commercetools.com/http-api-projects-stores#set-languages
77
     * @param StoreSetLanguagesAction|callable $action
78
     * @return $this
79
     */
80 1
    public function setLanguages($action = null)
81
    {
82 1
        $this->addAction($this->resolveAction(StoreSetLanguagesAction::class, $action));
83 1
        return $this;
84
    }
85
86
    /**
87
     * @link https://docs.commercetools.com/http-api-projects-stores#set-name
88
     * @param StoreSetNameAction|callable $action
89
     * @return $this
90
     */
91 1
    public function setName($action = null)
92
    {
93 1
        $this->addAction($this->resolveAction(StoreSetNameAction::class, $action));
94 1
        return $this;
95
    }
96
97
    /**
98
     * @link https://docs.commercetools.com/api/projects/stores#set-supply-channels
99
     * @param StoreSetSupplyChannelsAction|callable $action
100
     * @return $this
101
     */
102 1
    public function setSupplyChannels($action = null)
103
    {
104 1
        $this->addAction($this->resolveAction(StoreSetSupplyChannelsAction::class, $action));
105 1
        return $this;
106
    }
107
108
    /**
109
     * @return StoresActionBuilder
110
     */
111
    public static 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
    /**
170
     * @param array $actions
171
     * @return $this
172
     */
173
    public function setActions(array $actions)
174
    {
175
        $this->actions = $actions;
176
        return $this;
177
    }
178
}
179