|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jayS-de <[email protected]> |
|
4
|
|
|
* @created: 26.01.15, 17:22 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Commercetools\Core\Request; |
|
8
|
|
|
|
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Commercetools\Core\Client\HttpMethod; |
|
11
|
|
|
use Commercetools\Core\Client\JsonEndpoint; |
|
12
|
|
|
use Commercetools\Core\Client\JsonRequest; |
|
13
|
|
|
use Commercetools\Core\Error\Message; |
|
14
|
|
|
use Commercetools\Core\Model\Common\Context; |
|
15
|
|
|
use Commercetools\Core\Model\Common\ContextAwareInterface; |
|
16
|
|
|
use Commercetools\Core\Response\ResourceResponse; |
|
17
|
|
|
use Commercetools\Core\Error\UpdateActionLimitException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @package Commercetools\Core\Request |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractUpdateRequest extends AbstractApiRequest |
|
23
|
|
|
{ |
|
24
|
|
|
use ExpandTrait; |
|
25
|
|
|
|
|
26
|
|
|
const ACTION = 'action'; |
|
27
|
|
|
const ACTIONS = 'actions'; |
|
28
|
|
|
const VERSION = 'version'; |
|
29
|
|
|
const ACTION_MAX_LIMIT = 500; |
|
30
|
|
|
const ACTION_WARNING_TRESHOLD = 400; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $id; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var int |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $version; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var AbstractAction[] |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $actions = []; |
|
46
|
|
|
|
|
47
|
|
|
protected $overLimit = false; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param JsonEndpoint $endpoint |
|
51
|
|
|
* @param string $id |
|
52
|
|
|
* @param int $version |
|
53
|
|
|
* @param array $actions |
|
54
|
|
|
* @param Context $context |
|
55
|
|
|
*/ |
|
56
|
427 |
|
public function __construct($endpoint, $id, $version, array $actions = [], Context $context = null) |
|
57
|
|
|
{ |
|
58
|
427 |
|
parent::__construct($endpoint, $context); |
|
59
|
427 |
|
$this->setId($id)->setVersion($version)->setActions($actions); |
|
60
|
427 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return AbstractAction[] |
|
64
|
|
|
*/ |
|
65
|
367 |
|
public function getActions() |
|
66
|
|
|
{ |
|
67
|
367 |
|
return $this->actions; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function hasActions() |
|
74
|
|
|
{ |
|
75
|
1 |
|
return (0 !== count($this->actions)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param AbstractAction[] $actions |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
427 |
|
public function setActions(array $actions) |
|
83
|
|
|
{ |
|
84
|
427 |
|
$this->actions = []; |
|
85
|
427 |
|
foreach ($actions as $action) { |
|
86
|
|
|
if ($action instanceof AbstractAction) { |
|
87
|
|
|
$this->addAction($action); |
|
88
|
|
|
} elseif (is_array($action)) { |
|
89
|
|
|
$this->addActionAsArray($action); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
427 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param AbstractAction $action |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
353 |
|
public function addAction(AbstractAction $action) |
|
101
|
|
|
{ |
|
102
|
353 |
|
$this->actions[] = $action; |
|
103
|
353 |
|
if ($action instanceof ContextAwareInterface) { |
|
104
|
353 |
|
$action->setContextIfNull($this->getContextCallback()); |
|
105
|
|
|
} |
|
106
|
353 |
|
$this->checkActionLimit(); |
|
107
|
|
|
|
|
108
|
353 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param array $action |
|
113
|
|
|
* @return $this |
|
114
|
|
|
*/ |
|
115
|
8 |
|
public function addActionAsArray(array $action) |
|
116
|
|
|
{ |
|
117
|
8 |
|
$this->actions[] = $action; |
|
118
|
8 |
|
$this->checkActionLimit(); |
|
119
|
|
|
|
|
120
|
8 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
361 |
|
protected function checkActionLimit() |
|
124
|
|
|
{ |
|
125
|
361 |
|
if (count($this->actions) > static::ACTION_MAX_LIMIT) { |
|
126
|
2 |
|
$message = sprintf( |
|
127
|
2 |
|
Message::UPDATE_ACTION_LIMIT, |
|
128
|
2 |
|
$this->getPath(), |
|
129
|
2 |
|
static::ACTION_WARNING_TRESHOLD |
|
130
|
|
|
); |
|
131
|
2 |
|
throw new UpdateActionLimitException($message); |
|
132
|
|
|
} |
|
133
|
361 |
|
if (!$this->overLimit && count($this->actions) > static::ACTION_WARNING_TRESHOLD) { |
|
134
|
6 |
|
$this->overLimit = true; |
|
135
|
6 |
|
$logger = $this->getContext()->getLogger(); |
|
136
|
6 |
|
if (!is_null($logger)) { |
|
137
|
2 |
|
$message = sprintf( |
|
138
|
2 |
|
Message::UPDATE_ACTION_LIMIT_WARNING, |
|
139
|
2 |
|
$this->getPath(), |
|
140
|
2 |
|
static::ACTION_WARNING_TRESHOLD |
|
141
|
|
|
); |
|
142
|
2 |
|
$logger->warning($message); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
361 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
371 |
|
public function getId() |
|
151
|
|
|
{ |
|
152
|
371 |
|
return $this->id; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $id |
|
157
|
|
|
* @return $this |
|
158
|
|
|
*/ |
|
159
|
427 |
|
public function setId($id) |
|
160
|
|
|
{ |
|
161
|
427 |
|
$this->id = $id; |
|
162
|
|
|
|
|
163
|
427 |
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return int |
|
168
|
|
|
*/ |
|
169
|
372 |
|
public function getVersion() |
|
170
|
|
|
{ |
|
171
|
372 |
|
return $this->version; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param int $version |
|
176
|
|
|
* @return $this |
|
177
|
|
|
*/ |
|
178
|
427 |
|
public function setVersion($version) |
|
179
|
|
|
{ |
|
180
|
427 |
|
$this->version = $version; |
|
181
|
|
|
|
|
182
|
427 |
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return string |
|
187
|
|
|
* @internal |
|
188
|
|
|
*/ |
|
189
|
348 |
|
protected function getPath() |
|
190
|
|
|
{ |
|
191
|
348 |
|
return (string)$this->getEndpoint() . '/' . $this->getId() . $this->getParamString(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return JsonRequest |
|
196
|
|
|
* @internal |
|
197
|
|
|
*/ |
|
198
|
365 |
|
public function httpRequest() |
|
199
|
|
|
{ |
|
200
|
365 |
|
$payload = [static::VERSION => $this->getVersion(), static::ACTIONS => $this->getActions()]; |
|
201
|
365 |
|
return new JsonRequest(HttpMethod::POST, $this->getPath(), $payload); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param ResponseInterface $response |
|
206
|
|
|
* @return ResourceResponse |
|
207
|
|
|
* @internal |
|
208
|
|
|
*/ |
|
209
|
345 |
|
public function buildResponse(ResponseInterface $response) |
|
210
|
|
|
{ |
|
211
|
345 |
|
return new ResourceResponse($response, $this, $this->getContext()); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|