This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace FMUP\Import; |
||
3 | |||
4 | use FMUP\Import\Config\ConfigObjet; |
||
5 | use FMUP\Import\Config\Field; |
||
6 | use FMUP\Import\Config\Field\Validator\Required; |
||
7 | |||
8 | /** |
||
9 | * Répresente une line d'un fichier |
||
10 | * |
||
11 | * @author csanz |
||
12 | * |
||
13 | */ |
||
14 | class Config |
||
15 | { |
||
16 | |||
17 | /* |
||
18 | * *************************** |
||
19 | * ATTRIBUTS |
||
20 | * *************************** |
||
21 | */ |
||
22 | /** |
||
23 | * Liste des champs |
||
24 | * |
||
25 | * @var Field[] |
||
26 | */ |
||
27 | private $fieldList = array(); |
||
28 | |||
29 | /** |
||
30 | * liste des erreurs |
||
31 | * |
||
32 | * @var string[] |
||
33 | */ |
||
34 | private $errors = array(); |
||
35 | |||
36 | /** |
||
37 | * Ligne correspondant au premier doublon |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | private $duplicatedLine; |
||
42 | |||
43 | /** |
||
44 | * Liste des objets config |
||
45 | * |
||
46 | * @var ConfigObjet[] |
||
47 | */ |
||
48 | private $configList = array(); |
||
49 | |||
50 | /* |
||
51 | * *************************** |
||
52 | * SETTERS |
||
53 | * *************************** |
||
54 | */ |
||
55 | |||
56 | /** |
||
57 | * Ajoute un Field à la liste de la config |
||
58 | * |
||
59 | * @param Field $field |
||
60 | * @return $this |
||
61 | */ |
||
62 | 1 | public function addField(Field $field) |
|
63 | { |
||
64 | 1 | array_push($this->fieldList, $field); |
|
65 | 1 | if ($field->getRequired()) { |
|
66 | 1 | $field->addValidator(new Required()); |
|
67 | } |
||
68 | 1 | return $this; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Ajoute un ConfigObjet |
||
73 | * |
||
74 | * @param ConfigObjet $configObject |
||
75 | * @return $this |
||
76 | */ |
||
77 | 1 | public function addConfigObjet(ConfigObjet $configObject) |
|
78 | { |
||
79 | 1 | array_push($this->configList, $configObject); |
|
80 | 1 | return $this; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * |
||
85 | * @param int $line |
||
86 | * @return $this |
||
87 | */ |
||
88 | 1 | public function setDoublonLigne($line) |
|
89 | { |
||
90 | 1 | $this->duplicatedLine = (int)$line; |
|
91 | 1 | return $this; |
|
92 | } |
||
93 | |||
94 | /* |
||
95 | * *************************** |
||
96 | * GETTERS |
||
97 | * *************************** |
||
98 | */ |
||
99 | |||
100 | /** |
||
101 | * |
||
102 | * @return \FMUP\Import\Config\Field[] |
||
103 | */ |
||
104 | 2 | public function getListeField() |
|
105 | { |
||
106 | 2 | return $this->fieldList; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * |
||
111 | * @param int $index |
||
112 | * @return \FMUP\Import\Config\Field|null |
||
113 | */ |
||
114 | 1 | public function getField($index) |
|
115 | { |
||
116 | 1 | $index = (int)$index; |
|
117 | 1 | return isset($this->fieldList[$index]) ? $this->fieldList[$index] : null; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * |
||
122 | * @return \FMUP\Import\Config\ConfigObjet[] |
||
123 | */ |
||
124 | 1 | public function getListeConfigObjet() |
|
125 | { |
||
126 | 1 | return $this->configList; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * |
||
131 | * @return int |
||
132 | */ |
||
133 | 1 | public function getDoublonLigne() |
|
134 | { |
||
135 | 1 | return (int)$this->duplicatedLine; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * |
||
140 | * @return string[] |
||
141 | */ |
||
142 | 2 | public function getErrors() |
|
143 | { |
||
144 | 2 | return $this->errors; |
|
145 | } |
||
146 | |||
147 | /* |
||
148 | * *************************** |
||
149 | * FONCTIONS SPECIFIQUES |
||
150 | * *************************** |
||
151 | */ |
||
152 | |||
153 | /** |
||
154 | * Appelle tous les formatters et validators de chaque champ |
||
155 | * Renvoi true si tous les champs sont valides, false sinon |
||
156 | * |
||
157 | * Les erreurs sont stockées dans l'attribut $errors |
||
158 | * |
||
159 | * @return boolean |
||
160 | */ |
||
161 | 1 | public function validateLine() |
|
162 | { |
||
163 | // Réinitialisation du tableau d'erreur |
||
164 | 1 | $this->errors = array(); |
|
165 | 1 | foreach ($this->getListeField() as $field) { |
|
166 | 1 | $validField = $field->validateField(); |
|
167 | 1 | if (!$validField) { |
|
168 | 1 | $this->errors[$field->getName()] = "non valide"; |
|
169 | } |
||
170 | } |
||
171 | 1 | $validLine = count($this->errors) > 0 ? false : true; |
|
172 | 1 | if ($validLine) { |
|
173 | 1 | $this->validateObjects(); |
|
174 | } |
||
175 | 1 | return $validLine; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param $a |
||
180 | * @param $b |
||
181 | * @return int |
||
182 | * @usedby self::usort |
||
183 | * @SuppressWarnings(PMD.UnusedPrivateMethod) |
||
184 | */ |
||
185 | 1 | private function sortByPriority($a, $b) |
|
186 | { |
||
187 | /** |
||
188 | * @var $a \FMUP\Import\Config\ConfigObjet |
||
189 | * @var $b \FMUP\Import\Config\ConfigObjet |
||
190 | */ |
||
191 | 1 | if ($a->getPriorite() == $b->getPriorite()) { |
|
192 | 1 | return 0; |
|
193 | } |
||
194 | 1 | return ($a->getPriorite() < $b->getPriorite()) ? -1 : 1; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param $configList |
||
199 | * @uses self::sortByPriority |
||
200 | * @codeCoverageIgnore |
||
201 | */ |
||
202 | protected function usort($configList) |
||
203 | { |
||
204 | usort($configList, array($this, 'sortByPriority')); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @return $this |
||
209 | */ |
||
210 | 1 | public function validateObjects() |
|
211 | { |
||
212 | 1 | $configList = $this->getListeConfigObjet(); |
|
213 | // on trie le tableau par priorité |
||
214 | 1 | $this->usort($configList); |
|
215 | // pour chaque configObject |
||
216 | 1 | foreach ($configList as $configObject) { |
|
217 | 1 | $objectName = $configObject->getNomObjet(); |
|
218 | // On créé un objet du type donnée |
||
219 | /** @var \Model $objectInstance */ |
||
220 | 1 | $objectInstance = $this->createNomObject($objectName); |
|
221 | 1 | $where = array(); |
|
222 | // pour tous les champs renseignés |
||
223 | 1 | View Code Duplication | foreach ($configObject->getListeIndexChamp() as $index) { |
0 ignored issues
–
show
|
|||
224 | // on hydrate l'objet |
||
225 | 1 | $field = $this->getField($index); |
|
226 | 1 | $objectInstance->setAttribute($field->getChampCible(), $field->getValue()); |
|
227 | // et on prépare le filtre |
||
228 | 1 | $where[$field->getChampCible()] = $field->getChampCible() . " LIKE '%" . $field->getValue() . "%'"; |
|
229 | } |
||
230 | // on va chercher l'objet en base |
||
231 | 1 | if (!$objectInstance->findFirst($where)) { |
|
232 | // si on l'a pas trouvé, il va falloir l'insérer |
||
233 | 1 | $configObject->setStatutInsertion(); |
|
234 | } else { |
||
235 | // sinon on va mettre à jours |
||
236 | 1 | $configObject->setStatutMaj(); |
|
237 | } |
||
238 | } |
||
239 | 1 | return $this; |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * Génère des objets à partir de la line |
||
244 | * puis les rentres en base |
||
245 | * Via un insert si on ne l'a pas trouvé en base |
||
246 | * Via un update si on l'a trouvé |
||
247 | * @return $this |
||
248 | * @throws Exception |
||
249 | * @throws \FMUP\Exception |
||
250 | */ |
||
251 | 2 | public function insertLine() |
|
252 | { |
||
253 | 2 | $ids = array(); |
|
254 | 2 | $configList = $this->getListeConfigObjet(); |
|
255 | // on trie le tableau par priorité |
||
256 | 2 | $this->usort($configList); |
|
257 | // pour chaque configObject |
||
258 | 2 | foreach ($configList as $configObject) { |
|
259 | 2 | $objectName = $configObject->getNomObjet(); |
|
260 | // On créé un objet du type donnée |
||
261 | /* @var $objectInstance \Model */ |
||
262 | 2 | $objectInstance = $this->createNomObject($objectName); |
|
263 | 2 | $where = array(); |
|
264 | |||
265 | // Si on a besoin d'un id, on va le chercher dans le tableau |
||
266 | 2 | $attributeList = $configObject->getNomAttribut(); |
|
267 | 2 | foreach ($configObject->getIdNecessaire() as $mandatoryId) { |
|
268 | 1 | if (isset($ids[$mandatoryId]) && isset($attributeList[$mandatoryId])) { |
|
269 | // on le set |
||
270 | 1 | $objectInstance->setAttribute($attributeList[$mandatoryId], $ids[$mandatoryId]); |
|
271 | // et on prépare le filtre |
||
272 | 1 | $where[$attributeList[$mandatoryId]] = $attributeList[$mandatoryId] . " LIKE '%" |
|
273 | 1 | . $ids[$mandatoryId] . "%'"; |
|
274 | } |
||
275 | } |
||
276 | // pour tous les champs obligatoires renseignés |
||
277 | 2 | View Code Duplication | foreach ($configObject->getListeIndexChamp() as $index) { |
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
278 | // on hydrate l'objet |
||
279 | 1 | $field = $this->getField($index); |
|
280 | 1 | $objectInstance->setAttribute($field->getChampCible(), $field->getValue()); |
|
281 | // et on prépare le filtre |
||
282 | 1 | $where[$field->getChampCible()] = $field->getChampCible() . " LIKE '%" . $field->getValue() . "%'"; |
|
283 | } |
||
284 | // on hydrate toutes les infos sur l'objet |
||
285 | 2 | $this->hydrateInstance($objectInstance, $objectName); |
|
286 | // on va chercher l'objet en base |
||
287 | 2 | $foundInstance = $objectInstance->findFirst($where); |
|
288 | |||
289 | 2 | if (!$foundInstance) { |
|
290 | // si on l'a pas trouvé, on l'enregiste et on récupère l'id |
||
291 | 2 | $result = $objectInstance->save(); |
|
292 | 2 | if ($result) { |
|
293 | 1 | $ids[$objectName] = $result; |
|
294 | } else { |
||
295 | 2 | throw new Exception(implode(';', $objectInstance->getErrors())); |
|
296 | } |
||
297 | } else { |
||
298 | // sinon on récupère directement l'id |
||
299 | 1 | $ids[$objectName] = $foundInstance->getId(); |
|
300 | |||
301 | // puis on met à jours |
||
302 | 1 | $objectInstance->setAttribute("Id", $foundInstance->getId()); |
|
303 | 1 | $objectInstance->save(); |
|
304 | } |
||
305 | } |
||
306 | 1 | return $this; |
|
307 | } |
||
308 | |||
309 | /** |
||
310 | * @param \Model $objectInstance |
||
311 | * @param $objectName |
||
312 | * @return $this |
||
313 | */ |
||
314 | 2 | protected function hydrateInstance($objectInstance, $objectName) |
|
315 | { |
||
316 | /** @var $objectInstance \Model */ |
||
317 | 2 | foreach ($this->getListeField() as $field) { |
|
318 | 1 | if (\FMUP\StringHandling::toCamelCase($field->getTableCible()) == $objectName) { |
|
319 | 1 | $objectInstance->setAttribute($field->getChampCible(), $field->getValue()); |
|
320 | } |
||
321 | } |
||
322 | 2 | return $this; |
|
323 | } |
||
324 | |||
325 | /** |
||
326 | * @param string $objectName |
||
327 | * @return mixed |
||
328 | * @codeCoverageIgnore |
||
329 | */ |
||
330 | protected function createNomObject($objectName) |
||
331 | { |
||
332 | return new $objectName(); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | 1 | public function __toString() |
|
340 | { |
||
341 | 1 | $string = ''; |
|
342 | 1 | foreach ($this->getListeField() as $key => $field) { |
|
343 | 1 | $string .= $key . " \t" . $field->getName() . " \t" . $field->getValue() . " \t" . PHP_EOL; |
|
344 | } |
||
345 | 1 | return $string; |
|
346 | } |
||
347 | } |
||
348 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.