1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControleOnline\Service; |
4
|
|
|
|
5
|
|
|
use ControleOnline\Entity\ExtraData; |
6
|
|
|
use ControleOnline\Entity\ExtraFields; |
7
|
|
|
use Symfony\Component\Security\Core\Security; |
|
|
|
|
8
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
|
|
|
9
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class ExtraDataService |
16
|
|
|
{ |
17
|
|
|
private static $persisted = false; |
18
|
|
|
private $request; |
19
|
|
|
public function __construct( |
20
|
|
|
private EntityManagerInterface $manager, |
21
|
|
|
private RequestStack $requestStack, |
22
|
|
|
private Security $security, |
23
|
|
|
) { |
24
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function getUserIp() |
28
|
|
|
{ |
29
|
|
|
return $this->request->getClientIp(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function discoveryIdentifier($entity) |
33
|
|
|
{ |
34
|
|
|
$identifier = $this->request->headers->get('identifier') ?: $this->getUserIp(); |
35
|
|
|
|
36
|
|
|
if (method_exists($entity, 'setIdentifier')) { |
37
|
|
|
$entity->setIdentifier($identifier); |
38
|
|
|
$this->manager->persist($entity); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function discoveryUser($entity) |
43
|
|
|
{ |
44
|
|
|
if (method_exists($entity, 'setUser')) { |
45
|
|
|
$entity->setUser($this->security->getUser()); |
46
|
|
|
$this->manager->persist($entity); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function persist($entity) |
51
|
|
|
{ |
52
|
|
|
if (self::$persisted == true) |
|
|
|
|
53
|
|
|
return; |
54
|
|
|
self::$persisted = true; |
55
|
|
|
|
56
|
|
|
//$this->manager->persist($entity); |
57
|
|
|
//$this->manager->flush(); |
58
|
|
|
$this->persistData($entity); |
59
|
|
|
} |
60
|
|
|
private function persistData($entity = null) |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
$json = json_decode($this->request->getContent(), true); |
64
|
|
|
$extra_data = isset($json['extra-data']) ? $json['extra-data'] : null; |
65
|
|
|
|
66
|
|
|
if (!$extra_data) |
67
|
|
|
return; |
68
|
|
|
|
69
|
|
|
if ($entity) { |
70
|
|
|
$entity_id = $entity->getId(); |
71
|
|
|
$entity_name = (new \ReflectionClass($entity::class))->getShortName(); |
72
|
|
|
$this->discoveryIdentifier($entity); |
73
|
|
|
$this->discoveryUser($entity); |
74
|
|
|
} else { |
75
|
|
|
$entity_id = $extra_data['entity_id']; |
76
|
|
|
$entity_name = $extra_data['entity_name']; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
if (!$entity_id || !$entity_name) |
81
|
|
|
return; |
82
|
|
|
|
83
|
|
|
foreach ($extra_data['data'] as $key => $data) { |
84
|
|
|
$extra_fields = $this->manager->getRepository(ExtraFields::class)->find($key); |
85
|
|
|
|
86
|
|
|
$extraData = $this->manager->getRepository(ExtraData::class)->findOneBy([ |
87
|
|
|
'entity_id' => $entity_id, |
88
|
|
|
'entity_name' => $entity_name, |
89
|
|
|
'extra_fields' => $extra_fields |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
if (!$extraData) |
93
|
|
|
$extraData = new ExtraData(); |
94
|
|
|
|
95
|
|
|
$extraData->setExtraFields($extra_fields); |
96
|
|
|
$extraData->setEntityName($entity_name); |
97
|
|
|
$extraData->setEntityId($entity_id); |
98
|
|
|
$extraData->setValue($data); |
99
|
|
|
$this->manager->persist($extraData); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
$this->manager->flush(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function noChange() |
107
|
|
|
{ |
108
|
|
|
if (self::$persisted == true) |
|
|
|
|
109
|
|
|
return; |
110
|
|
|
$this->persistData(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths