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