1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jenschude <[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
|
717 |
|
public function __construct($endpoint, $id, $version, array $actions = [], Context $context = null) |
57
|
|
|
{ |
58
|
717 |
|
parent::__construct($endpoint, $context); |
59
|
717 |
|
$this->setId($id)->setVersion($version)->setActions($actions); |
60
|
717 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return AbstractAction[] |
64
|
|
|
*/ |
65
|
589 |
|
public function getActions() |
66
|
|
|
{ |
67
|
589 |
|
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
|
717 |
|
public function setActions(array $actions) |
83
|
|
|
{ |
84
|
717 |
|
$this->actions = []; |
85
|
717 |
|
foreach ($actions as $action) { |
86
|
54 |
|
if ($action instanceof AbstractAction) { |
87
|
54 |
|
$this->addAction($action); |
88
|
|
|
} elseif (is_array($action)) { |
89
|
|
|
$this->addActionAsArray($action); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
717 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param AbstractAction $action |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
575 |
|
public function addAction(AbstractAction $action) |
101
|
|
|
{ |
102
|
575 |
|
$this->actions[] = $action; |
103
|
575 |
|
if ($action instanceof ContextAwareInterface) { |
|
|
|
|
104
|
575 |
|
$action->setContextIfNull($this->getContextCallback()); |
105
|
|
|
} |
106
|
575 |
|
$this->checkActionLimit(); |
107
|
|
|
|
108
|
575 |
|
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
|
583 |
|
protected function checkActionLimit() |
124
|
|
|
{ |
125
|
583 |
|
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
|
583 |
|
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
|
583 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
588 |
|
public function getId() |
151
|
|
|
{ |
152
|
588 |
|
return $this->id; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $id |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
717 |
|
public function setId($id) |
160
|
|
|
{ |
161
|
717 |
|
$this->id = $id; |
162
|
|
|
|
163
|
717 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return int |
168
|
|
|
*/ |
169
|
594 |
|
public function getVersion() |
170
|
|
|
{ |
171
|
594 |
|
return $this->version; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param int $version |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
717 |
|
public function setVersion($version) |
179
|
|
|
{ |
180
|
717 |
|
$this->version = $version; |
181
|
|
|
|
182
|
717 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return string |
187
|
|
|
* @internal |
188
|
|
|
*/ |
189
|
563 |
|
protected function getPath() |
190
|
|
|
{ |
191
|
563 |
|
return (string)$this->getEndpoint() . '/' . $this->getId() . $this->getParamString(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return JsonRequest |
196
|
|
|
* @internal |
197
|
|
|
*/ |
198
|
586 |
|
public function httpRequest() |
199
|
|
|
{ |
200
|
586 |
|
$payload = [static::VERSION => $this->getVersion(), static::ACTIONS => $this->getActions()]; |
201
|
586 |
|
return new JsonRequest(HttpMethod::POST, $this->getPath(), $payload); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param ResponseInterface $response |
206
|
|
|
* @return ResourceResponse |
207
|
|
|
* @internal |
208
|
|
|
*/ |
209
|
567 |
|
public function buildResponse(ResponseInterface $response) |
210
|
|
|
{ |
211
|
567 |
|
return new ResourceResponse($response, $this, $this->getContext()); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|