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

ChannelsActionBuilder::addAction()   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\Channels\Command\ChannelAddRolesAction;
8
use Commercetools\Core\Request\Channels\Command\ChannelChangeDescriptionAction;
9
use Commercetools\Core\Request\Channels\Command\ChannelChangeKeyAction;
10
use Commercetools\Core\Request\Channels\Command\ChannelChangeNameAction;
11
use Commercetools\Core\Request\Channels\Command\ChannelRemoveRolesAction;
12
use Commercetools\Core\Request\Channels\Command\ChannelSetAddressAction;
13
use Commercetools\Core\Request\Channels\Command\ChannelSetCustomFieldAction;
14
use Commercetools\Core\Request\Channels\Command\ChannelSetCustomTypeAction;
15
use Commercetools\Core\Request\Channels\Command\ChannelSetGeoLocation;
16
use Commercetools\Core\Request\Channels\Command\ChannelSetRolesAction;
17
18
class ChannelsActionBuilder
19
{
20
    private $actions = [];
21
22
    /**
23
     * @link https://docs.commercetools.com/http-api-projects-channels.html#add-roles
24
     * @param ChannelAddRolesAction|callable $action
25
     * @return $this
26
     */
27 1
    public function addRoles($action = null)
28
    {
29 1
        $this->addAction($this->resolveAction(ChannelAddRolesAction::class, $action));
30 1
        return $this;
31
    }
32
33
    /**
34
     * @link https://docs.commercetools.com/http-api-projects-channels.html#change-description
35
     * @param ChannelChangeDescriptionAction|callable $action
36
     * @return $this
37
     */
38 1
    public function changeDescription($action = null)
39
    {
40 1
        $this->addAction($this->resolveAction(ChannelChangeDescriptionAction::class, $action));
41 1
        return $this;
42
    }
43
44
    /**
45
     * @link https://docs.commercetools.com/http-api-projects-channels.html#change-key
46
     * @param ChannelChangeKeyAction|callable $action
47
     * @return $this
48
     */
49 1
    public function changeKey($action = null)
50
    {
51 1
        $this->addAction($this->resolveAction(ChannelChangeKeyAction::class, $action));
52 1
        return $this;
53
    }
54
55
    /**
56
     * @link https://docs.commercetools.com/http-api-projects-channels.html#change-name
57
     * @param ChannelChangeNameAction|callable $action
58
     * @return $this
59
     */
60 1
    public function changeName($action = null)
61
    {
62 1
        $this->addAction($this->resolveAction(ChannelChangeNameAction::class, $action));
63 1
        return $this;
64
    }
65
66
    /**
67
     * @link https://docs.commercetools.com/http-api-projects-channels.html#remove-roles
68
     * @param ChannelRemoveRolesAction|callable $action
69
     * @return $this
70
     */
71 1
    public function removeRoles($action = null)
72
    {
73 1
        $this->addAction($this->resolveAction(ChannelRemoveRolesAction::class, $action));
74 1
        return $this;
75
    }
76
77
    /**
78
     * @link https://docs.commercetools.com/http-api-projects-channels.html#set-address
79
     * @param ChannelSetAddressAction|callable $action
80
     * @return $this
81
     */
82 1
    public function setAddress($action = null)
83
    {
84 1
        $this->addAction($this->resolveAction(ChannelSetAddressAction::class, $action));
85 1
        return $this;
86
    }
87
88
    /**
89
     *
90
     * @param ChannelSetCustomFieldAction|callable $action
91
     * @return $this
92
     */
93 1
    public function setCustomField($action = null)
94
    {
95 1
        $this->addAction($this->resolveAction(ChannelSetCustomFieldAction::class, $action));
96 1
        return $this;
97
    }
98
99
    /**
100
     *
101
     * @param ChannelSetCustomTypeAction|callable $action
102
     * @return $this
103
     */
104 1
    public function setCustomType($action = null)
105
    {
106 1
        $this->addAction($this->resolveAction(ChannelSetCustomTypeAction::class, $action));
107 1
        return $this;
108
    }
109
110
    /**
111
     * @link https://docs.commercetools.com/http-api-projects-channels.html#set-geolocation
112
     * @param ChannelSetGeoLocation|callable $action
113
     * @return $this
114
     */
115 1
    public function setGeoLocation($action = null)
116
    {
117 1
        $this->addAction($this->resolveAction(ChannelSetGeoLocation::class, $action));
118 1
        return $this;
119
    }
120
121
    /**
122
     * @link https://docs.commercetools.com/http-api-projects-channels.html#set-roles
123
     * @param ChannelSetRolesAction|callable $action
124
     * @return $this
125
     */
126 1
    public function setRoles($action = null)
127
    {
128 1
        $this->addAction($this->resolveAction(ChannelSetRolesAction::class, $action));
129 1
        return $this;
130
    }
131
132
    /**
133
     * @return ChannelsActionBuilder
134
     */
135
    public 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
     * @param array $actions
194
     * @return $this
195
     */
196
    public function setActions(array $actions)
197
    {
198
        $this->actions = $actions;
199
        return $this;
200
    }
201
}
202