|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of gpupo/cnova-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 <https://opensource.gpupo.com/>. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Gpupo\CnovaSdk\Entity\Product; |
|
16
|
|
|
|
|
17
|
|
|
use Gpupo\CnovaSdk\Entity\ManagerAbstract; |
|
18
|
|
|
use Gpupo\CommonSdk\Entity\EntityInterface; |
|
19
|
|
|
use Gpupo\CommonSdk\Traits\PoolTrait; |
|
20
|
|
|
|
|
21
|
|
|
class Manager extends ManagerAbstract |
|
22
|
|
|
{ |
|
23
|
|
|
use PoolTrait; |
|
24
|
|
|
|
|
25
|
|
|
protected $entity = 'Product'; |
|
26
|
|
|
|
|
27
|
|
|
protected $maps = [ |
|
28
|
|
|
'save' => ['POST', '/loads/products'], |
|
29
|
|
|
'updateStatus' => ['PUT', '/sellerItems/{itemId}/status'], //Ativação/Desativação de produto no Marketplace |
|
30
|
|
|
'updateStock' => ['PUT', '/sellerItems/{itemId}/stock'], //Atualização do estoque do item |
|
31
|
|
|
'updatePrice' => ['PUT', '/sellerItems/{itemId}/prices'], //Atualização do preço do item |
|
32
|
|
|
'findById' => ['GET', '/loads/products/{itemId}'], |
|
33
|
|
|
'fetch' => ['GET', '/sellerItems?_offset={offset}&_limit={limit}&status={status}&createdAt={createdAt}'], |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
3 |
|
public function update(EntityInterface $entity, EntityInterface $existent) |
|
40
|
|
|
{ |
|
41
|
3 |
|
parent::update($entity, $existent); |
|
42
|
|
|
|
|
43
|
3 |
|
$updated = []; |
|
44
|
|
|
|
|
45
|
|
|
foreach ([ |
|
46
|
3 |
|
'Stock', |
|
47
|
|
|
'Price', |
|
48
|
|
|
//'Status' |
|
49
|
|
|
] as $key) { |
|
50
|
3 |
|
$getter = 'get'.$key; |
|
51
|
3 |
|
if ($this->attributesDiff($entity->$getter(), $existent->$getter())) { |
|
52
|
1 |
|
$method = 'update'.$key; |
|
53
|
3 |
|
$updated[$key] = $this->$method($entity); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
$atualizado = !empty($updated); |
|
58
|
|
|
|
|
59
|
|
|
$context = [ |
|
60
|
3 |
|
'id' => $entity->getId(), |
|
61
|
3 |
|
'atualizado' => $atualizado, |
|
62
|
3 |
|
'atributos' => $updated, |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
3 |
|
$this->log('info', 'Operação de Atualização de entity ' |
|
66
|
3 |
|
.$this->entity, $context); |
|
67
|
|
|
|
|
68
|
3 |
|
return $atualizado; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
5 |
|
public function save(EntityInterface $product, $route = 'save') |
|
72
|
|
|
{ |
|
73
|
5 |
|
$existent = $product->getPrevious(); |
|
74
|
|
|
|
|
75
|
5 |
|
$this->log('INFO', 'save', ['route' => $route, 'existent' => $existent]); |
|
76
|
|
|
|
|
77
|
5 |
|
if ($existent) { |
|
78
|
3 |
|
return $this->update($product, $existent); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
return $this->getPool()->add($product); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
protected function getMap($route, Product $product) |
|
85
|
|
|
{ |
|
86
|
1 |
|
return $this->factoryMap($route, ['itemId' => $product->getId()]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function updatePrice(Product $product) |
|
90
|
|
|
{ |
|
91
|
|
|
$map = $this->getMap('updatePrice', $product); |
|
92
|
|
|
|
|
93
|
|
|
return $this->execute($map, $product->getPrice()->toJson()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
protected function updateStock(Product $product) |
|
97
|
|
|
{ |
|
98
|
1 |
|
$map = $this->getMap('updateStock', $product); |
|
99
|
|
|
|
|
100
|
1 |
|
return $this->execute($map, $product->getStock()->toJson()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function updateStatus(Product $product) |
|
104
|
|
|
{ |
|
105
|
|
|
$map = $this->getMap('updateStatus', $product); |
|
106
|
|
|
|
|
107
|
|
|
return $this->execute($map, $product->getStock()->toJson()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
public function commit() |
|
111
|
|
|
{ |
|
112
|
1 |
|
if ($this->getPool()->count() > 0) { |
|
113
|
1 |
|
return $this->execute($this->factoryMap('save'), $this->getPool()->toJson()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|