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
|
|
|
final 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->log('info', '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->log('error', $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
|
|
|
try { |
137
|
|
|
$p = $this->findById($entity->getId()); |
138
|
|
|
if ($p instanceof Item) { |
139
|
|
|
return $p; |
140
|
|
|
} |
141
|
|
|
} catch (\Exception $e) { |
142
|
|
|
throw new \Exception('Sku #'.$entity->getId().' not found on marketplace!'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
1 |
|
public function update(EntityInterface $entity, EntityInterface $existent = null) |
150
|
|
|
{ |
151
|
1 |
|
parent::update($entity, $existent); |
152
|
|
|
|
153
|
1 |
|
if (empty($existent)) { |
154
|
|
|
$existent = $this->resolvePrevious($entity); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$response = [ |
158
|
1 |
|
'sku' => $entity->getId(), |
159
|
|
|
'bypassed' => [], |
160
|
|
|
'code' => [], |
161
|
|
|
'updated' => [], |
162
|
|
|
]; |
163
|
|
|
|
164
|
|
|
$list = [ |
165
|
1 |
|
'updateDetails', |
166
|
|
|
]; |
167
|
|
|
|
168
|
1 |
|
if (true === $this->strategy['info']) { |
169
|
|
|
$list[] = 'updateInfo'; |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
foreach ($list as $method) { |
173
|
1 |
|
$response = $this->$method($entity, $existent, $response); |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
$this->log('info', 'Operação de Atualização', $response); |
177
|
|
|
|
178
|
1 |
|
return $response; |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
public function updateInfo(Item $entity, $existent = null, array $response = []) |
182
|
|
|
{ |
183
|
2 |
|
$compare = $this->attributesDiff($entity, $existent, ['name', 'color', |
184
|
|
|
'size', 'gender', 'eanIsbn', 'images', 'video', 'height', |
185
|
|
|
'width', 'depth', 'weight', ]); |
186
|
|
|
|
187
|
2 |
|
if (false === $compare) { |
188
|
1 |
|
$response['bypassed'][] = 'info'; |
189
|
|
|
|
190
|
1 |
|
return $response; |
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
$map = $this->factoryMap('update', ['itemId' => $entity->getId(), 'sku' => $entity->getId()]); |
194
|
1 |
|
$operation = $this->execute($map, $entity->toJson()); |
195
|
1 |
|
$response['code']['info'] = $operation->getHttpStatusCode(); |
196
|
1 |
|
$response['updated'][] = 'info'; |
197
|
|
|
|
198
|
1 |
|
return $response; |
199
|
|
|
} |
200
|
|
|
|
201
|
2 |
|
public function updateDetails(Item $entity, Item $existent = null, array $response = []) |
202
|
|
|
{ |
203
|
|
|
$list = [ |
204
|
2 |
|
'Status' => ['active'], |
205
|
|
|
'Stock' => ['available'], |
206
|
|
|
'Price' => ['price'], |
207
|
|
|
]; |
208
|
|
|
|
209
|
2 |
|
if ($this->strategy['pricing'] === 'schedule') { |
210
|
|
|
$list['PriceSchedule'] = ['priceTo']; |
211
|
|
|
} |
212
|
|
|
|
213
|
2 |
|
foreach ($list as $key => $attributes) { |
214
|
2 |
|
$getter = 'get'.$key; |
215
|
|
|
|
216
|
2 |
|
if (!empty($existent)) { |
217
|
2 |
|
if (false === $this->attributesDiff($entity->$getter(), $existent->$getter(), $attributes)) { |
218
|
1 |
|
$response['bypassed'][] = $key; |
219
|
1 |
|
continue; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
1 |
|
$response['code'][$key] = $this->saveDetail($entity, $key)->getHttpStatusCode(); |
224
|
1 |
|
$response['updated'][] = $key; |
225
|
|
|
} |
226
|
|
|
|
227
|
2 |
|
return $response; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
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.