1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControleOnline\Service; |
4
|
|
|
|
5
|
|
|
use ControleOnline\Entity\Device; |
6
|
|
|
use ControleOnline\Entity\DeviceConfig; |
7
|
|
|
use ControleOnline\Entity\ExtraData; |
8
|
|
|
use ControleOnline\Entity\ExtraFields; |
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface |
|
|
|
|
10
|
|
|
as Security; |
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
|
|
|
12
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
13
|
|
|
|
14
|
|
|
class ExtraDataService |
15
|
|
|
{ |
16
|
|
|
private static $persisted = false; |
17
|
|
|
private $request; |
18
|
|
|
public function __construct( |
19
|
|
|
private EntityManagerInterface $manager, |
20
|
|
|
private RequestStack $requestStack, |
21
|
|
|
private Security $security, |
22
|
|
|
private DeviceService $deviceService, |
23
|
|
|
private SkyNetService $skyNetService |
24
|
|
|
|
25
|
|
|
) { |
26
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function getEntityByExtraData(ExtraFields $extraFields, string $code, object | string $entity) |
31
|
|
|
{ |
32
|
|
|
$class = $this->getEntityName($entity); |
33
|
|
|
$extraData = $this->manager->getRepository(ExtraData::class)->findOneBy([ |
34
|
|
|
'extra_fields' => $extraFields, |
35
|
|
|
'entity_name' => $class->getShortName(), |
36
|
|
|
'value' => $code |
37
|
|
|
]); |
38
|
|
|
if ($extraData) |
39
|
|
|
return $this->manager->getRepository($class->getName())->find($extraData->getEntityId()); |
40
|
|
|
|
41
|
|
|
return null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
public function discoveryExtraData(int|string $entityId, ExtraFields $extraFields, string $code, object | string $entity) |
46
|
|
|
{ |
47
|
|
|
$class = $this->getEntityName($entity); |
48
|
|
|
|
49
|
|
|
$extraData = $this->getEntityByExtraData($extraFields, $code, $entity); |
50
|
|
|
if ($extraData) return $extraData; |
51
|
|
|
|
52
|
|
|
$extraData = new ExtraData(); |
53
|
|
|
$extraData->setEntityId($entityId); |
54
|
|
|
$extraData->setExtraFields($extraFields); |
55
|
|
|
$extraData->setValue($code); |
56
|
|
|
$extraData->setEntityName($class->getShortName()); |
57
|
|
|
$this->manager->persist($extraData); |
58
|
|
|
$this->manager->flush(); |
59
|
|
|
|
60
|
|
|
return $this->manager->getRepository($class->getName())->find($extraData->getEntityId()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function discoveryExtraFields(string $fieldName, string $context, ?string $configs = '{}', ?string $fieldType = 'text', ?bool $required = false): ExtraFields |
64
|
|
|
{ |
65
|
|
|
|
66
|
|
|
$extraFields = $this->manager->getRepository(ExtraFields::class)->findOneBy([ |
67
|
|
|
'name' => $fieldName, |
68
|
|
|
'type' => $fieldType, |
69
|
|
|
'context' => $context |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
if (!$extraFields) { |
73
|
|
|
$extraFields = new ExtraFields(); |
74
|
|
|
$extraFields->setName($fieldName); |
75
|
|
|
$extraFields->setContext($context); |
76
|
|
|
$extraFields->setConfigs($configs); |
77
|
|
|
$extraFields->setType($fieldType); |
|
|
|
|
78
|
|
|
$extraFields->setRequired($required); |
79
|
|
|
$this->manager->persist($extraFields); |
80
|
|
|
$this->manager->flush(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $extraFields; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
private function getUserIp() |
88
|
|
|
{ |
89
|
|
|
return $this->request->getClientIp(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function discoveryDevice(&$entity) |
93
|
|
|
{ |
94
|
|
|
if ($entity instanceof Device || $entity instanceof DeviceConfig || !$this->request->headers) |
95
|
|
|
return; |
96
|
|
|
|
97
|
|
|
$deviceId = $this->request->headers->get('DEVICE') ?: $this->getUserIp(); |
98
|
|
|
if (method_exists($entity, 'setDevice')) { |
99
|
|
|
if ($entity->getDevice()) return; |
100
|
|
|
$device = $this->deviceService->discoveryDevice($deviceId); |
101
|
|
|
$entity->setDevice($device); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function discoveryUser(&$entity) |
106
|
|
|
{ |
107
|
|
|
$token = $this->security->getToken(); |
108
|
|
|
$user = $token ? $token->getUser() : $this->skyNetService->getBotUser(); |
109
|
|
|
|
110
|
|
|
if (method_exists($entity, 'setUser') && !$entity->getUser()) |
111
|
|
|
$entity->setUser($user); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function persist(&$entity) |
115
|
|
|
{ |
116
|
|
|
if (self::$persisted == true) |
|
|
|
|
117
|
|
|
return; |
118
|
|
|
self::$persisted = true; |
119
|
|
|
|
120
|
|
|
//$this->manager->persist($entity); |
121
|
|
|
//$this->manager->flush(); |
122
|
|
|
$this->persistData($entity); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function getEntityName(object | string $entity): \ReflectionClass |
126
|
|
|
{ |
127
|
|
|
return (new \ReflectionClass($entity)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function persistData(&$entity = null) |
131
|
|
|
{ |
132
|
|
|
|
133
|
|
|
if ($entity) { |
134
|
|
|
$entity_id = $entity->getId(); |
135
|
|
|
$entity_name = $this->getEntityName($entity)->getShortName(); |
136
|
|
|
|
137
|
|
|
//$this->manager->persist($entity); |
138
|
|
|
} else { |
139
|
|
|
$json = json_decode($this->request->getContent(), true); |
140
|
|
|
$extra_data = isset($json['extra-data']) ? $json['extra-data'] : null; |
141
|
|
|
if (!$extra_data) |
142
|
|
|
return; |
143
|
|
|
$entity_id = $extra_data['entity_id']; |
144
|
|
|
$entity_name = $extra_data['entity_name']; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
if (!$entity_id || !$entity_name) |
149
|
|
|
return; |
150
|
|
|
|
151
|
|
|
if (!isset($extra_data) || !isset($extra_data['data'])) |
152
|
|
|
return; |
153
|
|
|
|
154
|
|
|
foreach ($extra_data['data'] as $key => $data) { |
155
|
|
|
$extra_fields = $this->manager->getRepository(ExtraFields::class)->find($key); |
156
|
|
|
|
157
|
|
|
$extraData = $this->manager->getRepository(ExtraData::class)->findOneBy([ |
158
|
|
|
'entity_id' => $entity_id, |
159
|
|
|
'entity_name' => $entity_name, |
160
|
|
|
'extra_fields' => $extra_fields |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
if (!$extraData) |
164
|
|
|
$extraData = new ExtraData(); |
165
|
|
|
|
166
|
|
|
$extraData->setExtraFields($extra_fields); |
167
|
|
|
$extraData->setEntityName($entity_name); |
168
|
|
|
$extraData->setEntityId($entity_id); |
169
|
|
|
$extraData->setValue($data); |
170
|
|
|
$this->manager->persist($extraData); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
$this->manager->flush(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function noChange() |
178
|
|
|
{ |
179
|
|
|
$this->persistData(); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
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