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
|
|
|
public function discoveryDevice(&$entity) |
33
|
|
|
{ |
34
|
|
|
$device = $this->request->headers->get('DEVICE') ?: $this->getUserIp(); |
35
|
|
|
|
36
|
|
|
if (method_exists($entity, 'setDevice')) { |
37
|
|
|
$entity->setDevice($device); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function discoveryUser(&$entity) |
42
|
|
|
{ |
43
|
|
|
if (method_exists($entity, 'setUser')) { |
44
|
|
|
$entity->setUser($this->security->getUser()); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function persist(&$entity) |
49
|
|
|
{ |
50
|
|
|
if (self::$persisted == true) |
|
|
|
|
51
|
|
|
return; |
52
|
|
|
self::$persisted = true; |
53
|
|
|
|
54
|
|
|
//$this->manager->persist($entity); |
55
|
|
|
//$this->manager->flush(); |
56
|
|
|
$this->persistData($entity); |
57
|
|
|
} |
58
|
|
|
private function persistData(&$entity = null) |
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
if ($entity) { |
62
|
|
|
$entity_id = $entity->getId(); |
63
|
|
|
$entity_name = (new \ReflectionClass($entity::class))->getShortName(); |
64
|
|
|
|
65
|
|
|
//$this->manager->persist($entity); |
66
|
|
|
} else { |
67
|
|
|
$json = json_decode($this->request->getContent(), true); |
68
|
|
|
$extra_data = isset($json['extra-data']) ? $json['extra-data'] : null; |
69
|
|
|
if (!$extra_data) |
70
|
|
|
return; |
71
|
|
|
$entity_id = $extra_data['entity_id']; |
72
|
|
|
$entity_name = $extra_data['entity_name']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
if (!$entity_id || !$entity_name) |
77
|
|
|
return; |
78
|
|
|
|
79
|
|
|
foreach ($extra_data['data'] as $key => $data) { |
|
|
|
|
80
|
|
|
$extra_fields = $this->manager->getRepository(ExtraFields::class)->find($key); |
81
|
|
|
|
82
|
|
|
$extraData = $this->manager->getRepository(ExtraData::class)->findOneBy([ |
83
|
|
|
'entity_id' => $entity_id, |
84
|
|
|
'entity_name' => $entity_name, |
85
|
|
|
'extra_fields' => $extra_fields |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
if (!$extraData) |
89
|
|
|
$extraData = new ExtraData(); |
90
|
|
|
|
91
|
|
|
$extraData->setExtraFields($extra_fields); |
92
|
|
|
$extraData->setEntityName($entity_name); |
93
|
|
|
$extraData->setEntityId($entity_id); |
94
|
|
|
$extraData->setValue($data); |
95
|
|
|
$this->manager->persist($extraData); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
$this->manager->flush(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function noChange() |
103
|
|
|
{ |
104
|
|
|
|
105
|
|
|
$this->persistData(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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