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; |
16
|
|
|
|
17
|
|
|
use Gpupo\CommonSchema\TranslatorDataCollection; |
18
|
|
|
use Gpupo\CommonSdk\Entity\EntityInterface; |
19
|
|
|
use Gpupo\CommonSdk\Entity\ManagerAbstract; |
20
|
|
|
use Gpupo\CommonSdk\Entity\ManagerInterface; |
21
|
|
|
|
22
|
|
|
abstract class AbstractManager extends ManagerAbstract implements ManagerInterface |
23
|
|
|
{ |
24
|
4 |
|
protected function fetchDefaultParameters() |
25
|
|
|
{ |
26
|
|
|
return [ |
27
|
4 |
|
'buId' => 'ZT', |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
15 |
|
public function factoryMap($operation, array $parameters = null) |
32
|
|
|
{ |
33
|
15 |
|
return parent::factoryMap($operation, array_merge($this->fetchDefaultParameters(), (array) $parameters)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @codeCoverageIgnore |
38
|
|
|
* |
39
|
|
|
* @return Gpupo\Common\Entity\CollectionAbstract|null|false |
40
|
|
|
*/ |
41
|
|
|
protected function fetchPrepare($data) |
42
|
|
|
{ |
43
|
|
|
if (empty($data)) { |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->factoryEntityCollection($data); |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
protected function factoryEntityCollection($data) |
51
|
|
|
{ |
52
|
4 |
|
return $this->factoryNeighborObject($this->getEntityName().'Collection', $data); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function save(EntityInterface $entity, $route = 'save') |
56
|
|
|
{ |
57
|
1 |
|
return $this->execute($this->factoryMap($route), $entity->toJson($route)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return Gpupo\Common\Entity\CollectionAbstract|false |
62
|
|
|
*/ |
63
|
3 |
|
public function findById($itemId) |
64
|
|
|
{ |
65
|
3 |
|
$data = parent::findById($itemId); |
66
|
|
|
|
67
|
3 |
|
if (empty($data) || $data->get('type') === 'Resource_Not_Found') { |
68
|
1 |
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
return $this->factoryEntity($data->toArray()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
* |
77
|
|
|
* @codeCoverageIgnore |
78
|
|
|
*/ |
79
|
|
|
public function update(EntityInterface $entity, EntityInterface $existent = null) |
80
|
|
|
{ |
81
|
|
|
$text = 'Chamada a Atualização de entity '.$this->entity; |
82
|
|
|
|
83
|
|
|
return $this->log('debug', $text, [ |
84
|
|
|
'entity' => $entity, |
85
|
|
|
'existent' => $existent, |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function factoryTranslator(array $data = []) |
90
|
|
|
{ |
91
|
|
|
throw new \Exception('factoryTranslator() deve ser implementado!'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function translatorUpdate(TranslatorDataCollection $data, TranslatorDataCollection $existent = null) |
95
|
|
|
{ |
96
|
|
|
$entity = $this->factoryTranslator($data)->translateFrom(); |
97
|
|
|
|
98
|
|
|
if (!empty($existent)) { |
99
|
|
|
$previous = $this->factoryTranslator($existent)->translateFrom(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->update($entity, empty($existent) ? $previous : null); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
public function translatorFetch() |
106
|
|
|
{ |
107
|
1 |
|
$dataCollection = new TranslatorDataCollection(); |
108
|
1 |
|
$collection = $this->fetch(); |
109
|
|
|
|
110
|
1 |
|
if (0 < $collection->count()) { |
111
|
1 |
|
foreach ($collection as $entity) { |
|
|
|
|
112
|
1 |
|
$dataCollection->add($this->factoryTranslator(['native' => $entity])->translateTo()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
return $dataCollection; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
public function translateFindById($itemId) |
120
|
|
|
{ |
121
|
1 |
|
$collection = $this->findById($itemId); |
122
|
|
|
|
123
|
1 |
|
if (empty($collection)) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
return $this->factoryTranslator(['native' => $collection])->translateTo(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: