Passed
Push — master ( ce99b8...8dba24 )
by Luiz Kim
03:01
created

ExtraDataService::getEntityName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
13
14
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...
15
use stdClass;
16
17
class ExtraDataService
18
{
19
    private static $persisted = false;
20
    private $request;
21
    public function __construct(
22
        private EntityManagerInterface $manager,
23
        private RequestStack $requestStack,
24
        private Security $security,
25
        private DeviceService $deviceService
26
27
    ) {
28
        $this->request = $requestStack->getCurrentRequest();
29
    }
30
31
32
    public function getEntityByExtraData(ExtraFields $extraFields, string $code, object | string $entity)
33
    {
34
        $class = $this->getEntityName($entity);
35
        $extraData = $this->manager->getRepository(ExtraData::class)->findOneBy([
36
            'extra_fields' => $extraFields,
37
            'entity_name' => $class->getShortName(),
38
            'value' => $code
39
        ]);
40
        if ($extraData)
41
            return $this->manager->getRepository($class::class)->find($extraData->getEntityId());
42
43
        return null;
44
    }
45
46
    public function discoveryExtraFields(string $fieldName, string $context, ?string $configs = '{}',  ?string $fieldType = 'text', ?bool $required = false): ExtraFields
47
    {
48
49
        $extraFields = $this->manager->getRepository(ExtraFields::class)->findOneBy([
50
            'name' => $fieldName,
51
            'type' => $fieldType,
52
            'context' => $context
53
        ]);
54
55
        if (!$extraFields)
56
            $extraFields = new ExtraFields();
57
58
        $extraFields->setName($fieldName);
59
        $extraFields->setContext($context);
60
        $extraFields->setConfigs($configs);
61
        $extraFields->setType($fieldType);
62
        $extraFields->setRequired($required);
63
        $this->manager->persist($extraFields);
64
        $this->manager->flush();
65
66
        return $extraFields;
67
    }
68
69
70
    private function getUserIp()
71
    {
72
        return $this->request->getClientIp();
73
    }
74
75
    public function discoveryDevice(&$entity)
76
    {
77
        if ($entity instanceof Device || $entity instanceof DeviceConfig || !$this->request->headers)
78
            return;
79
80
        $deviceId = $this->request->headers->get('DEVICE') ?: $this->getUserIp();
81
        if (method_exists($entity, 'setDevice')) {
82
            if ($entity->getDevice()) return;
83
            $device = $this->deviceService->discoveryDevice($deviceId);
84
            $entity->setDevice($device);
85
        }
86
    }
87
88
    public function discoveryUser(&$entity)
89
    {
90
        if (method_exists($entity, 'setUser') && !$entity->getUser() && $this->security->getToken())
91
            $entity->setUser($this->security->getToken()->getUser());
92
    }
93
94
    public function persist(&$entity)
95
    {
96
        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...
97
            return;
98
        self::$persisted = true;
99
100
        //$this->manager->persist($entity);
101
        //$this->manager->flush();
102
        $this->persistData($entity);
103
    }
104
105
    private function getEntityName(object | string $entity)
106
    {
107
        return (new \ReflectionClass($entity));
108
    }
109
110
    private function persistData(&$entity = null)
111
    {
112
113
        if ($entity) {
114
            $entity_id = $entity->getId();
115
            $entity_name = $this->getEntityName($entity)->getShortName();
116
117
            //$this->manager->persist($entity);
118
        } else {
119
            $json =       json_decode($this->request->getContent(), true);
120
            $extra_data = isset($json['extra-data']) ? $json['extra-data'] : null;
121
            if (!$extra_data)
122
                return;
123
            $entity_id = $extra_data['entity_id'];
124
            $entity_name = $extra_data['entity_name'];
125
        }
126
127
128
        if (!$entity_id || !$entity_name)
129
            return;
130
131
        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...
132
            $extra_fields = $this->manager->getRepository(ExtraFields::class)->find($key);
133
134
            $extraData = $this->manager->getRepository(ExtraData::class)->findOneBy([
135
                'entity_id' => $entity_id,
136
                'entity_name' => $entity_name,
137
                'extra_fields' => $extra_fields
138
            ]);
139
140
            if (!$extraData)
141
                $extraData = new ExtraData();
142
143
            $extraData->setExtraFields($extra_fields);
144
            $extraData->setEntityName($entity_name);
145
            $extraData->setEntityId($entity_id);
146
            $extraData->setValue($data);
147
            $this->manager->persist($extraData);
148
        }
149
150
151
        $this->manager->flush();
152
    }
153
154
    public function  noChange()
155
    {
156
        $this->persistData();
157
    }
158
}
159