Passed
Push — master ( 6e98f4...3ba681 )
by Luiz Kim
02:50
created

ExtraDataService::discoveryExtraData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 16
rs 9.9
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
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...e\TokenStorageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
as Security;
11
use Symfony\Component\HttpFoundation\RequestStack;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\RequestStack was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
24
    ) {
25
        $this->request = $requestStack->getCurrentRequest();
26
    }
27
28
29
    public function getEntityByExtraData(ExtraFields $extraFields, string $code, object | string $entity)
30
    {
31
        $class = $this->getEntityName($entity);
32
        $extraData = $this->manager->getRepository(ExtraData::class)->findOneBy([
33
            'extra_fields' => $extraFields,
34
            'entity_name' => $class->getShortName(),
35
            'value' => $code
36
        ]);
37
        if ($extraData)
38
            return $this->manager->getRepository($class::class)->find($extraData->getEntityId());
39
40
        return null;
41
    }
42
43
44
    public function discoveryExtraData(int $entityId, ExtraFields $extraFields, string $code, object | string $entity)
45
    {
46
        $class = $this->getEntityName($entity);
47
48
        $extraData = $this->getEntityByExtraData($extraFields,  $code,  $entity);
49
        if (!$extraData) {
50
            $extraData = new ExtraData();
51
            $extraData->setEntityId($entityId);
52
            $extraData->setExtraFields($extraFields);
53
            $extraData->setValue($code);
54
            $extraData->setEntityName($class->getShortName());
55
            $this->manager->persist($extraData);
56
            $this->manager->flush();
57
        }
58
59
        return $this->manager->getRepository($class::class)->find($extraData->getEntityId());
60
    }
61
62
    public function discoveryExtraFields(string $fieldName, string $context, ?string $configs = '{}',  ?string $fieldType = 'text', ?bool $required = false): ExtraFields
63
    {
64
65
        $extraFields = $this->manager->getRepository(ExtraFields::class)->findOneBy([
66
            'name' => $fieldName,
67
            'type' => $fieldType,
68
            'context' => $context
69
        ]);
70
71
        if (!$extraFields)
72
            $extraFields = new ExtraFields();
73
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
        return $extraFields;
83
    }
84
85
86
    private function getUserIp()
87
    {
88
        return $this->request->getClientIp();
89
    }
90
91
    public function discoveryDevice(&$entity)
92
    {
93
        if ($entity instanceof Device || $entity instanceof DeviceConfig || !$this->request->headers)
94
            return;
95
96
        $deviceId = $this->request->headers->get('DEVICE') ?: $this->getUserIp();
97
        if (method_exists($entity, 'setDevice')) {
98
            if ($entity->getDevice()) return;
99
            $device = $this->deviceService->discoveryDevice($deviceId);
100
            $entity->setDevice($device);
101
        }
102
    }
103
104
    public function discoveryUser(&$entity)
105
    {
106
        if (method_exists($entity, 'setUser') && !$entity->getUser() && $this->security->getToken())
107
            $entity->setUser($this->security->getToken()->getUser());
108
    }
109
110
    public function persist(&$entity)
111
    {
112
        if (self::$persisted == true)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
113
            return;
114
        self::$persisted = true;
115
116
        //$this->manager->persist($entity);
117
        //$this->manager->flush();
118
        $this->persistData($entity);
119
    }
120
121
    private function getEntityName(object | string $entity)
122
    {
123
        return (new \ReflectionClass($entity));
124
    }
125
126
    private function persistData(&$entity = null)
127
    {
128
129
        if ($entity) {
130
            $entity_id = $entity->getId();
131
            $entity_name = $this->getEntityName($entity)->getShortName();
132
133
            //$this->manager->persist($entity);
134
        } else {
135
            $json =       json_decode($this->request->getContent(), true);
136
            $extra_data = isset($json['extra-data']) ? $json['extra-data'] : null;
137
            if (!$extra_data)
138
                return;
139
            $entity_id = $extra_data['entity_id'];
140
            $entity_name = $extra_data['entity_name'];
141
        }
142
143
144
        if (!$entity_id || !$entity_name)
145
            return;
146
147
        foreach ($extra_data['data'] as $key => $data) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $extra_data does not seem to be defined for all execution paths leading up to this point.
Loading history...
148
            $extra_fields = $this->manager->getRepository(ExtraFields::class)->find($key);
149
150
            $extraData = $this->manager->getRepository(ExtraData::class)->findOneBy([
151
                'entity_id' => $entity_id,
152
                'entity_name' => $entity_name,
153
                'extra_fields' => $extra_fields
154
            ]);
155
156
            if (!$extraData)
157
                $extraData = new ExtraData();
158
159
            $extraData->setExtraFields($extra_fields);
160
            $extraData->setEntityName($entity_name);
161
            $extraData->setEntityId($entity_id);
162
            $extraData->setValue($data);
163
            $this->manager->persist($extraData);
164
        }
165
166
167
        $this->manager->flush();
168
    }
169
170
    public function  noChange()
171
    {
172
        $this->persistData();
173
    }
174
}
175