Test Failed
Push — develop ( b569c3...75fa7f )
by Jens
20:49 queued 14s
created

StoresActionBuilder::addDistributionChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
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 1
20
    /**
21 1
     * @link https://docs.commercetools.com/api/projects/stores#add-distribution-channel
22 1
     * @param StoreAddDistributionChannelAction|callable $action
23
     * @return $this
24
     */
25
    public function addDistributionChannel($action = null)
26
    {
27
        $this->addAction($this->resolveAction(StoreAddDistributionChannelAction::class, $action));
28
        return $this;
29
    }
30 1
31
    /**
32 1
     * @link https://docs.commercetools.com/api/projects/stores#add-supply-channel
33 1
     * @param StoreAddSupplyChannelAction|callable $action
34
     * @return $this
35
     */
36
    public function addSupplyChannel($action = null)
37
    {
38
        $this->addAction($this->resolveAction(StoreAddSupplyChannelAction::class, $action));
39
        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
    public function removeDistributionChannel($action = null)
48
    {
49
        $this->addAction($this->resolveAction(StoreRemoveDistributionChannelAction::class, $action));
50 2
        return $this;
51
    }
52 2
53 2
    /**
54 2
     * @link https://docs.commercetools.com/api/projects/stores#remove-supply-channel
55 2
     * @param StoreRemoveSupplyChannelAction|callable $action
56
     * @return $this
57 2
     */
58 2
    public function removeSupplyChannel($action = null)
59
    {
60
        $this->addAction($this->resolveAction(StoreRemoveSupplyChannelAction::class, $action));
61
        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
    public function setDistributionChannels($action = null)
70 2
    {
71
        $this->addAction($this->resolveAction(StoreSetDistributionChannelsAction::class, $action));
72 2
        return $this;
73
    }
74
75 2
    /**
76
     * @link https://docs.commercetools.com/http-api-projects-stores#set-languages
77
     * @param StoreSetLanguagesAction|callable $action
78
     * @return $this
79
     */
80
    public function setLanguages($action = null)
81
    {
82 2
        $this->addAction($this->resolveAction(StoreSetLanguagesAction::class, $action));
83
        return $this;
84 2
    }
85 2
86
    /**
87
     * @link https://docs.commercetools.com/http-api-projects-stores#set-name
88
     * @param StoreSetNameAction|callable $action
89
     * @return $this
90
     */
91 2
    public function setName($action = null)
92
    {
93 2
        $this->addAction($this->resolveAction(StoreSetNameAction::class, $action));
94
        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
    public function setSupplyChannels($action = null)
103
    {
104
        $this->addAction($this->resolveAction(StoreSetSupplyChannelsAction::class, $action));
105
        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
    private function resolveAction($class, $action = null)
123
    {
124
        if (is_null($action) || is_callable($action)) {
125
            $callback = $action;
126
            $emptyAction = $class::of();
127
            $action = $this->callback($emptyAction, $callback);
128
        }
129
        if ($action instanceof $class) {
130
            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
    private function callback($action, callable $callback = null)
143
    {
144
        if (!is_null($callback)) {
145
            $action = $callback($action);
146
        }
147
        return $action;
148
    }
149
150
    /**
151
     * @param AbstractAction $action
152
     * @return $this;
153
     */
154
    public function addAction(AbstractAction $action)
155
    {
156
        $this->actions[] = $action;
157
        return $this;
158
    }
159
160
    /**
161
     * @return array
162
     */
163
    public function getActions()
164
    {
165
        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