1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of gpupo component |
5
|
|
|
* Created by Gilmar Pupo <[email protected]> |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* For more information, see <http://www.g1mr.com/>. |
9
|
|
|
*/ |
10
|
|
|
namespace Gpupo\NetshoesSdk\Entity\Product; |
11
|
|
|
|
12
|
|
|
use Gpupo\CommonSdk\Entity\EntityInterface; |
13
|
|
|
use Gpupo\CommonSdk\Traits\PoolTrait; |
14
|
|
|
use Gpupo\NetshoesSdk\Entity\ManagerAbstract; |
15
|
|
|
|
16
|
|
|
class Manager extends ManagerAbstract |
17
|
|
|
{ |
18
|
|
|
use PoolTrait; |
19
|
|
|
|
20
|
|
|
protected $entity = 'Product'; |
21
|
|
|
|
22
|
|
|
protected $maps = [ |
23
|
|
|
'save' => ['POST', '/loads/products'], |
24
|
|
|
'updateStatus' => ['PUT', '/sellerItems/{itemId}/status'], //Ativação/Desativação de produto no Marketplace |
25
|
|
|
'updateStock' => ['PUT', '/sellerItems/{itemId}/stock'], //Atualização do estoque do item |
26
|
|
|
'updatePrice' => ['PUT', '/sellerItems/{itemId}/prices'], //Atualização do preço do item |
27
|
|
|
'findById' => ['GET', '/loads/products/{itemId}'], |
28
|
|
|
'fetch' => ['GET', '/sellerItems?_offset={offset}&_limit={limit}&status={status}&createdAt={createdAt}'], |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function update(EntityInterface $entity, EntityInterface $existent) |
35
|
|
|
{ |
36
|
|
|
parent::update($entity, $existent); |
37
|
|
|
|
38
|
|
|
$updated = []; |
39
|
|
|
|
40
|
|
|
foreach ([ |
41
|
|
|
'Stock', |
42
|
|
|
'Price', |
43
|
|
|
//'Status' |
44
|
|
|
] as $key) { |
45
|
|
|
$getter = 'get' . $key; |
46
|
|
|
if ($this->attributesDiff($entity->$getter(), $existent->$getter())) { |
47
|
|
|
$method = 'update' . $key; |
48
|
|
|
$updated[$key] = $this->$method($entity); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$atualizado = ! empty($updated); |
53
|
|
|
|
54
|
|
|
$context = [ |
55
|
|
|
'id' => $entity->getId(), |
56
|
|
|
'atualizado' => $atualizado, |
57
|
|
|
'atributos' => $updated, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$this->log('info', 'Operação de Atualização de entity ' |
61
|
|
|
. $this->entity, $context); |
62
|
|
|
|
63
|
|
|
return $atualizado; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function save(EntityInterface $product, $route = 'save') |
67
|
|
|
{ |
68
|
|
|
$existent = $product->getPrevious(); |
69
|
|
|
|
70
|
|
|
$this->log('INFO', 'save', ['route' => $route, 'existent' => $existent]); |
71
|
|
|
|
72
|
|
|
if ($existent) { |
73
|
|
|
return $this->update($product, $existent); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->getPool()->add($product); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function getMap($route, Product $product) |
80
|
|
|
{ |
81
|
|
|
return $this->factoryMap($route, ['itemId' => $product->getId()]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function updatePrice(Product $product) |
85
|
|
|
{ |
86
|
|
|
$map = $this->getMap('updatePrice', $product); |
87
|
|
|
|
88
|
|
|
return $this->execute($map, $product->getPrice()->toJson()); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function updateStock(Product $product) |
92
|
|
|
{ |
93
|
|
|
$map = $this->getMap('updateStock', $product); |
94
|
|
|
|
95
|
|
|
return $this->execute($map, $product->getStock()->toJson()); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function updateStatus(Product $product) |
99
|
|
|
{ |
100
|
|
|
$map = $this->getMap('updateStatus', $product); |
101
|
|
|
|
102
|
|
|
return $this->execute($map, $product->getStock()->toJson()); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function commit() |
106
|
|
|
{ |
107
|
|
|
if ($this->getPool()->count() > 0) { |
108
|
|
|
return $this->execute($this->factoryMap('save'), $this->getPool()->toJson()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: