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\CommonSchema\TranslatorDataCollection; |
18
|
|
|
use Gpupo\CommonSdk\Entity\EntityInterface; |
19
|
|
|
use Gpupo\NetshoesSdk\Entity\AbstractManager; |
20
|
|
|
use Gpupo\NetshoesSdk\Factory; |
21
|
|
|
|
22
|
|
|
class Manager extends AbstractManager |
23
|
|
|
{ |
24
|
|
|
protected $entity = 'Product'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @codeCoverageIgnore |
28
|
|
|
*/ |
29
|
|
|
protected $maps = [ |
30
|
|
|
'save' => ['POST', '/products'], |
31
|
|
|
'findById' => ['GET', '/products/{itemId}'], |
32
|
|
|
'patch' => ['PATCH', '/products/{itemId}'], |
33
|
|
|
'update' => ['PUT', '/products/{itemId}'], |
34
|
|
|
'fetch' => ['GET', '/products?page={offset}&size={limit}'], |
35
|
|
|
]; |
36
|
|
|
|
37
|
1 |
|
public function patch(EntityInterface $entity, $compare) |
38
|
|
|
{ |
39
|
1 |
|
if (empty($compare)) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
$json = json_encode($entity->toPatch($compare)); |
|
|
|
|
44
|
1 |
|
$map = $this->factoryMap('patch', ['itemId' => $entity->getId()]); |
45
|
1 |
|
$operation = $this->execute($map, $json); |
46
|
|
|
|
47
|
|
|
$feedback = [ |
48
|
1 |
|
'fields' => $compare, |
49
|
1 |
|
'response_code' => $operation->getHttpStatusCode(), |
50
|
|
|
]; |
51
|
|
|
|
52
|
1 |
|
$this->log('info', 'Operação de Atualização de produto (PATCH)', $feedback); |
53
|
|
|
|
54
|
1 |
|
return $feedback; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
2 |
|
public function update(EntityInterface $entity, EntityInterface $existent = null) |
61
|
|
|
{ |
62
|
2 |
|
if (0 === $entity->getSkus()->count()) { |
|
|
|
|
63
|
1 |
|
throw new \InvalidArgumentException('Product precisa conter SKU!'); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$response = []; |
67
|
|
|
|
68
|
1 |
|
$compare = $this->attributesDiff($entity, $existent, ['department', 'productType']); |
69
|
1 |
|
$response['patch'] = $this->patch($entity, $compare); |
70
|
1 |
|
$response['skus'] = []; |
71
|
|
|
|
72
|
1 |
|
$skuManager = $this->factorySubManager(Factory::getInstance(), 'sku'); |
73
|
|
|
|
74
|
1 |
|
foreach ($entity->getSkus() as $sku) { |
|
|
|
|
75
|
1 |
|
$previous = null; |
76
|
1 |
|
if ($existent) { |
77
|
1 |
|
$previous = $existent->getSkus()->findById($sku->getId()); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
$response['skus'][] = $skuManager->update($sku, $previous); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $response; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function factoryTranslator(array $data = []) |
87
|
|
|
{ |
88
|
|
|
$translator = new Translator($data); |
89
|
|
|
|
90
|
|
|
return $translator; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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: