|
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\States\Command\StateAddRolesAction; |
|
8
|
|
|
use Commercetools\Core\Request\States\Command\StateChangeInitialAction; |
|
9
|
|
|
use Commercetools\Core\Request\States\Command\StateChangeKeyAction; |
|
10
|
|
|
use Commercetools\Core\Request\States\Command\StateChangeTypeAction; |
|
11
|
|
|
use Commercetools\Core\Request\States\Command\StateRemoveRolesAction; |
|
12
|
|
|
use Commercetools\Core\Request\States\Command\StateSetDescriptionAction; |
|
13
|
|
|
use Commercetools\Core\Request\States\Command\StateSetNameAction; |
|
14
|
|
|
use Commercetools\Core\Request\States\Command\StateSetRolesAction; |
|
15
|
|
|
use Commercetools\Core\Request\States\Command\StateSetTransitionsAction; |
|
16
|
|
|
use Commercetools\Core\Request\States\Command\TransitionStateAction; |
|
17
|
|
|
|
|
18
|
|
|
class StatesActionBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
private $actions = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @link https://docs.commercetools.com/http-api-projects-states.html#add-state-roles |
|
24
|
|
|
* @param StateAddRolesAction|callable $action |
|
25
|
|
|
* @return $this |
|
26
|
|
|
*/ |
|
27
|
1 |
|
public function addRoles($action = null) |
|
28
|
|
|
{ |
|
29
|
1 |
|
$this->addAction($this->resolveAction(StateAddRolesAction::class, $action)); |
|
30
|
1 |
|
return $this; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @link https://docs.commercetools.com/http-api-projects-states.html#change-initial-state |
|
35
|
|
|
* @param StateChangeInitialAction|callable $action |
|
36
|
|
|
* @return $this |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function changeInitial($action = null) |
|
39
|
|
|
{ |
|
40
|
1 |
|
$this->addAction($this->resolveAction(StateChangeInitialAction::class, $action)); |
|
41
|
1 |
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @link https://docs.commercetools.com/http-api-projects-states.html#change-state-key |
|
46
|
|
|
* @param StateChangeKeyAction|callable $action |
|
47
|
|
|
* @return $this |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function changeKey($action = null) |
|
50
|
|
|
{ |
|
51
|
1 |
|
$this->addAction($this->resolveAction(StateChangeKeyAction::class, $action)); |
|
52
|
1 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @link https://docs.commercetools.com/http-api-projects-states.html#change-state-type |
|
57
|
|
|
* @param StateChangeTypeAction|callable $action |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function changeType($action = null) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->addAction($this->resolveAction(StateChangeTypeAction::class, $action)); |
|
63
|
1 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @link https://docs.commercetools.com/http-api-projects-states.html#remove-state-roles |
|
68
|
|
|
* @param StateRemoveRolesAction|callable $action |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function removeRoles($action = null) |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->addAction($this->resolveAction(StateRemoveRolesAction::class, $action)); |
|
74
|
1 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @link https://docs.commercetools.com/http-api-projects-states.html#set-state-description |
|
79
|
|
|
* @param StateSetDescriptionAction|callable $action |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function setDescription($action = null) |
|
83
|
|
|
{ |
|
84
|
1 |
|
$this->addAction($this->resolveAction(StateSetDescriptionAction::class, $action)); |
|
85
|
1 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @link https://docs.commercetools.com/http-api-projects-states.html#set-state-name |
|
90
|
|
|
* @param StateSetNameAction|callable $action |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function setName($action = null) |
|
94
|
|
|
{ |
|
95
|
1 |
|
$this->addAction($this->resolveAction(StateSetNameAction::class, $action)); |
|
96
|
1 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @link https://docs.commercetools.com/http-api-projects-states.html#set-state-roles |
|
101
|
|
|
* @param StateSetRolesAction|callable $action |
|
102
|
|
|
* @return $this |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function setRoles($action = null) |
|
105
|
|
|
{ |
|
106
|
1 |
|
$this->addAction($this->resolveAction(StateSetRolesAction::class, $action)); |
|
107
|
1 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @link https://docs.commercetools.com/http-api-projects-states.html#set-transitions |
|
112
|
|
|
* @param StateSetTransitionsAction|callable $action |
|
113
|
|
|
* @return $this |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function setTransitions($action = null) |
|
116
|
|
|
{ |
|
117
|
1 |
|
$this->addAction($this->resolveAction(StateSetTransitionsAction::class, $action)); |
|
118
|
1 |
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* |
|
123
|
|
|
* @param TransitionStateAction|callable $action |
|
124
|
|
|
* @return $this |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function transitionState($action = null) |
|
127
|
|
|
{ |
|
128
|
1 |
|
$this->addAction($this->resolveAction(TransitionStateAction::class, $action)); |
|
129
|
1 |
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return StatesActionBuilder |
|
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
|
|
|
|