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; |
16
|
|
|
|
17
|
|
|
use Gpupo\CommonSdk\Entity\EntityInterface; |
18
|
|
|
use Gpupo\CommonSdk\Exception\InvalidArgumentException; |
19
|
|
|
use Gpupo\CommonSdk\Exception\RuntimeException; |
20
|
|
|
use Gpupo\CommonSdk\Traits\TranslatorManagerTrait; |
21
|
|
|
use Gpupo\NetshoesSdk\Entity\AbstractManager; |
22
|
|
|
use Gpupo\NetshoesSdk\Factory; |
23
|
|
|
|
24
|
|
|
final class Manager extends AbstractManager |
25
|
|
|
{ |
26
|
|
|
use TranslatorManagerTrait; |
27
|
|
|
|
28
|
|
|
protected $entity = 'Product'; |
29
|
|
|
|
30
|
|
|
protected $strategy = [ |
31
|
|
|
'info' => false, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @codeCoverageIgnore |
36
|
|
|
*/ |
37
|
|
|
protected $maps = [ |
38
|
|
|
'save' => ['POST', '/products'], |
39
|
|
|
'findById' => ['GET', '/products/{itemId}'], |
40
|
|
|
'patch' => ['PATCH', '/products/{itemId}'], |
41
|
|
|
'update' => ['PUT', '/products/{itemId}'], |
42
|
|
|
'fetch' => ['GET', '/products?page={offset}&size={limit}'], |
43
|
|
|
'statusById' => ['GET', '/skus/{itemId}/bus/{buId}/status'], |
44
|
|
|
]; |
45
|
|
|
|
46
|
1 |
|
public function patch(EntityInterface $entity, $compare) |
47
|
|
|
{ |
48
|
1 |
|
if (empty($compare)) { |
49
|
1 |
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$json = json_encode($entity->toPatch($compare)); |
|
|
|
|
53
|
1 |
|
$map = $this->factoryMap('patch', ['itemId' => $entity->getId()]); |
54
|
1 |
|
$operation = $this->execute($map, $json); |
55
|
|
|
|
56
|
|
|
$feedback = [ |
57
|
1 |
|
'fields' => $compare, |
58
|
1 |
|
'response_code' => $operation->getHttpStatusCode(), |
59
|
|
|
]; |
60
|
|
|
|
61
|
1 |
|
$this->log('info', 'Operação de Atualização de produto (PATCH)', $feedback); |
62
|
|
|
|
63
|
1 |
|
return $feedback; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function resolveNew(EntityInterface $entity) |
67
|
|
|
{ |
68
|
|
|
$this->save($entity); |
69
|
|
|
|
70
|
|
|
return [ |
71
|
|
|
'created' => true, |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function fetchStatusById($itemId) |
76
|
|
|
{ |
77
|
|
|
$response = $this->perform($this->factoryMap('statusById', [ |
78
|
|
|
'itemId' => $itemId, |
79
|
|
|
])); |
80
|
|
|
|
81
|
|
|
$data = $this->processResponse($response); |
82
|
|
|
|
83
|
|
|
if (empty($data)) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new Status($data->toArray()); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
private function skuManager() |
91
|
|
|
{ |
92
|
1 |
|
return $this->factorySubManager(Factory::getInstance(), 'sku'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
2 |
|
public function update(EntityInterface $entity, EntityInterface $existent = null) |
99
|
|
|
{ |
100
|
2 |
|
if (0 === $entity->getSkus()->count()) { |
|
|
|
|
101
|
1 |
|
throw new InvalidArgumentException('Product precisa conter SKU!'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
try { |
105
|
1 |
|
$status = $entity->get('status'); |
106
|
1 |
|
if (!$status instanceof Status) { |
107
|
|
|
$status = $this->fetchStatusById($entity->getId()); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
if (true === $status->isPending()) { |
111
|
1 |
|
return ['pending' => true]; |
112
|
|
|
} |
113
|
|
|
} catch (RuntimeException $e) { |
114
|
|
|
if (404 === $e->getCode()) { |
115
|
|
|
return $this->resolveNew($entity); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
$response = []; |
120
|
|
|
|
121
|
1 |
|
if (true === $this->strategy['info']) { |
122
|
|
|
$compare = $this->attributesDiff($entity, $existent, ['department', 'productType']); |
123
|
|
|
$response['patch'] = $this->patch($entity, $compare); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
$response['skus'] = []; |
127
|
|
|
|
128
|
1 |
|
foreach ($entity->getSkus() as $sku) { |
|
|
|
|
129
|
1 |
|
$previous = null; |
130
|
|
|
|
131
|
1 |
|
if ($existent instanceof EntityInterface) { |
132
|
1 |
|
$previous = $existent->getSkus()->findById($sku->getId()); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
$response['skus'][] = $this->skuManager()->update($sku, $previous); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return $response; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
public function factoryTranslator(array $data = []) |
142
|
|
|
{ |
143
|
1 |
|
$translator = new Translator($data); |
144
|
|
|
|
145
|
1 |
|
return $translator; |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
public function findById($itemId) |
149
|
|
|
{ |
150
|
2 |
|
$product = parent::findById($itemId); |
151
|
|
|
|
152
|
2 |
|
if (empty($product)) { |
153
|
1 |
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
$sm = $this->skuManager(); |
157
|
1 |
|
$product->getSkus()->forAll(function ($key, $element) use ($sm) { |
158
|
1 |
|
$sm->hydrate($element); |
159
|
1 |
|
}); |
160
|
|
|
|
161
|
1 |
|
return $product; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: