|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of gpupo/netshoes-sdk |
|
5
|
|
|
* Created by Gilmar Pupo <[email protected]> |
|
6
|
|
|
* For the information of copyright and license you should read the file |
|
7
|
|
|
* LICENSE which is distributed with this source code. |
|
8
|
|
|
* Para a informação dos direitos autorais e de licença você deve ler o arquivo |
|
9
|
|
|
* LICENSE que é distribuído com este código-fonte. |
|
10
|
|
|
* Para obtener la información de los derechos de autor y la licencia debe leer |
|
11
|
|
|
* el archivo LICENSE que se distribuye con el código fuente. |
|
12
|
|
|
* For more information, see <http://www.g1mr.com/>. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Gpupo\NetshoesSdk\Entity\Product\Sku; |
|
16
|
|
|
|
|
17
|
|
|
use Gpupo\CommonSdk\Entity\EntityInterface; |
|
18
|
|
|
use Gpupo\CommonSdk\Exception\ManagerException; |
|
19
|
|
|
use Gpupo\NetshoesSdk\Entity\AbstractManager; |
|
20
|
|
|
|
|
21
|
|
|
class Manager extends AbstractManager |
|
22
|
|
|
{ |
|
23
|
|
|
protected $entity = 'Sku'; |
|
24
|
|
|
|
|
25
|
|
|
protected $strategy = [ |
|
26
|
|
|
'info' => false, |
|
27
|
|
|
'pricing' => 'simples', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @codeCoverageIgnore |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function setUp() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->maps = include 'map.config.php'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return Gpupo\Common\Entity\CollectionAbstract|null |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function findById($itemId) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$response = $this->perform($this->factoryMap('findById', [ |
|
44
|
1 |
|
'productId' => $itemId, |
|
45
|
1 |
|
'itemId' => $itemId, |
|
46
|
|
|
])); |
|
47
|
|
|
|
|
48
|
1 |
|
$data = $this->processResponse($response); |
|
49
|
|
|
|
|
50
|
1 |
|
if (empty($data)) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
$sku = new Item($data->toArray()); |
|
55
|
|
|
|
|
56
|
1 |
|
return $this->hydrate($sku); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function getDetail(EntityInterface $sku, $type) |
|
60
|
|
|
{ |
|
61
|
|
|
$response = $this->perform($this->factoryMap('get'.$type, ['sku' => $sku->getId()])); |
|
62
|
|
|
$className = 'Gpupo\NetshoesSdk\Entity\Product\Sku\\'.$type; |
|
63
|
|
|
$data = $this->processResponse($response); |
|
64
|
|
|
|
|
65
|
|
|
$o = new $className($data->toArray()); |
|
66
|
|
|
|
|
67
|
|
|
$this->getLogger()->addInfo('Detail', [ |
|
68
|
|
|
'sku' => $sku->getId(), |
|
69
|
|
|
'typ' => $type, |
|
70
|
|
|
'response' => $data, |
|
71
|
|
|
'className' => $className, |
|
72
|
|
|
'object' => $o, |
|
73
|
|
|
]); |
|
74
|
|
|
|
|
75
|
|
|
return $o; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function getPriceScheduleCollection(EntityInterface $sku) |
|
79
|
|
|
{ |
|
80
|
|
|
try { |
|
81
|
|
|
$response = $this->perform($this->factoryMap('getPriceSchedule', ['sku' => $sku->getId()])); |
|
82
|
|
|
$data = $this->processResponse($response); |
|
83
|
|
|
|
|
84
|
|
|
if (empty($data)) { |
|
85
|
|
|
throw new ManagerException('No price schedule on SKU #'.$sku->getId()); |
|
86
|
|
|
} |
|
87
|
|
|
} catch (ManagerException $e) { |
|
88
|
|
|
$this->getLogger()->addError($e->getMessage()); |
|
89
|
|
|
|
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$collection = new PriceScheduleCollection($data->toArray()); |
|
94
|
|
|
|
|
95
|
|
|
return $collection; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function add(EntityInterface $entity, $productId) |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->execute($this->factoryMap('add', [ |
|
101
|
|
|
'productId' => $productId, |
|
102
|
|
|
]), $entity->toJson()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
public function saveDetail(Item $sku, $type) |
|
106
|
|
|
{ |
|
107
|
1 |
|
$json = $sku->toJson($type); |
|
108
|
1 |
|
$map = $this->factoryMap('save'.$type, ['sku' => $sku->getId()]); |
|
109
|
|
|
|
|
110
|
1 |
|
return $this->execute($map, $json); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function hydratePriceSchedule(EntityInterface $sku) |
|
114
|
|
|
{ |
|
115
|
|
|
$ps = $this->getPriceScheduleCollection($sku); |
|
116
|
|
|
|
|
117
|
|
|
$sku->setPriceSchedule(false); |
|
|
|
|
|
|
118
|
|
|
if ($ps instanceof PriceScheduleCollection) { |
|
119
|
|
|
$sku->setPriceSchedule($ps->getCurrent()); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $sku; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function hydrate(EntityInterface $sku) |
|
126
|
|
|
{ |
|
127
|
|
|
$sku->setPrice($this->getDetail($sku, 'Price')) |
|
|
|
|
|
|
128
|
|
|
->setStock($this->getDetail($sku, 'Stock')) |
|
129
|
|
|
->setStatus($this->getDetail($sku, 'Status')); |
|
130
|
|
|
|
|
131
|
|
|
return $this->hydratePriceSchedule($sku); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
protected function resolvePrevious(Item $entity) |
|
135
|
|
|
{ |
|
136
|
|
|
$p = $this->findById($entity->getId()); |
|
137
|
|
|
|
|
138
|
|
|
if ($p instanceof Item) { |
|
139
|
|
|
return $p; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
throw new \Exception('Sku #'.$entity->getId().' not found on marketplace!'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
1 |
|
public function update(EntityInterface $entity, EntityInterface $existent = null) |
|
149
|
|
|
{ |
|
150
|
1 |
|
parent::update($entity, $existent); |
|
151
|
|
|
|
|
152
|
1 |
|
if (empty($existent)) { |
|
153
|
|
|
$existent = $this->resolvePrevious($entity); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$response = [ |
|
157
|
1 |
|
'sku' => $entity->getId(), |
|
158
|
|
|
'bypassed' => [], |
|
159
|
|
|
'code' => [], |
|
160
|
|
|
'updated' => [], |
|
161
|
|
|
]; |
|
162
|
|
|
|
|
163
|
|
|
$list = [ |
|
164
|
1 |
|
'updateDetails', |
|
165
|
|
|
]; |
|
166
|
|
|
|
|
167
|
1 |
|
if (true === $this->strategy['info']) { |
|
168
|
|
|
$list[] = 'updateInfo'; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
1 |
|
foreach ($list as $method) { |
|
172
|
1 |
|
$response = $this->$method($entity, $existent, $response); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
1 |
|
$this->log('info', 'Operação de Atualização', $response); |
|
176
|
|
|
|
|
177
|
1 |
|
return $response; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
2 |
|
public function updateInfo(Item $entity, $existent = null, array $response = []) |
|
181
|
|
|
{ |
|
182
|
2 |
|
$compare = $this->attributesDiff($entity, $existent, ['name', 'color', |
|
183
|
|
|
'size', 'gender', 'eanIsbn', 'images', 'video', 'height', |
|
184
|
|
|
'width', 'depth', 'weight', ]); |
|
185
|
|
|
|
|
186
|
2 |
|
if (false === $compare) { |
|
187
|
1 |
|
$response['bypassed'][] = 'info'; |
|
188
|
|
|
|
|
189
|
1 |
|
return $response; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
1 |
|
$map = $this->factoryMap('update', ['itemId' => $entity->getId(), 'sku' => $entity->getId()]); |
|
193
|
1 |
|
$operation = $this->execute($map, $entity->toJson()); |
|
194
|
1 |
|
$response['code']['info'] = $operation->getHttpStatusCode(); |
|
195
|
1 |
|
$response['updated'][] = 'info'; |
|
196
|
|
|
|
|
197
|
1 |
|
return $response; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
2 |
|
public function updateDetails(Item $entity, Item $existent = null, array $response = []) |
|
201
|
|
|
{ |
|
202
|
|
|
$list = [ |
|
203
|
2 |
|
'Status' => ['active'], |
|
204
|
|
|
'Stock' => ['available'], |
|
205
|
|
|
'Price' => ['price'], |
|
206
|
|
|
]; |
|
207
|
|
|
|
|
208
|
2 |
|
if ($this->strategy['pricing'] === 'schedule') { |
|
209
|
|
|
$list['PriceSchedule'] = ['priceTo']; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
2 |
|
foreach ($list as $key => $attributes) { |
|
213
|
2 |
|
$getter = 'get'.$key; |
|
214
|
|
|
|
|
215
|
2 |
|
if (!empty($existent)) { |
|
216
|
2 |
|
if (false === $this->attributesDiff($entity->$getter(), $existent->$getter(), $attributes)) { |
|
217
|
1 |
|
$response['bypassed'][] = $key; |
|
218
|
1 |
|
continue; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
1 |
|
$response['code'][$key] = $this->saveDetail($entity, $key)->getHttpStatusCode(); |
|
223
|
1 |
|
$response['updated'][] = $key; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
2 |
|
return $response; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.