Passed
Push — master ( 65c462...83eae8 )
by Luiz Kim
02:34
created

ExtraDataService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 4
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\Serializer\SerializerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Serializer\SerializerInterface 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 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...
13
14
15
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...
16
17
18
class ExtraDataService
19
{
20
    private static $persisted = false;
21
    private $request;
22
    public function __construct(
23
        private EntityManagerInterface $manager,
24
        private RequestStack $requestStack,
25
        private Security $security,
26
        private DeviceService $deviceService
27
28
    ) {
29
        $this->request = $requestStack->getCurrentRequest();
30
    }
31
32
    private function getUserIp()
33
    {
34
        return $this->request->getClientIp();
35
    }
36
37
    public function discoveryDevice(&$entity)
38
    {
39
        if ($entity instanceof Device || $entity instanceof DeviceConfig || !$this->request->headers)
40
            return;
41
42
        $deviceId = $this->request->headers->get('DEVICE') ?: $this->getUserIp();
43
        if (method_exists($entity, 'setDevice')) {
44
            if ($entity->getDevice()) return;
45
            $device = $this->deviceService->discoveryDevice($deviceId);
46
            $entity->setDevice($device);
47
        }
48
    }
49
50
    public function discoveryUser(&$entity)
51
    {
52
        if (method_exists($entity, 'setUser') && !$entity->getUser() && $this->security->getToken())
53
            $entity->setUser($this->security->getToken()->getUser());
54
    }
55
56
    public function persist(&$entity)
57
    {
58
        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...
59
            return;
60
        self::$persisted = true;
61
62
        //$this->manager->persist($entity);
63
        //$this->manager->flush();
64
        $this->persistData($entity);
65
    }
66
    private function persistData(&$entity = null)
67
    {
68
69
        if ($entity) {
70
            $entity_id = $entity->getId();
71
            $entity_name = (new \ReflectionClass($entity::class))->getShortName();
72
73
            //$this->manager->persist($entity);
74
        } else {
75
            $json =       json_decode($this->request->getContent(), true);
76
            $extra_data = isset($json['extra-data']) ? $json['extra-data'] : null;
77
            if (!$extra_data)
78
                return;
79
            $entity_id = $extra_data['entity_id'];
80
            $entity_name = $extra_data['entity_name'];
81
        }
82
83
84
        if (!$entity_id || !$entity_name)
85
            return;
86
87
        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...
88
            $extra_fields = $this->manager->getRepository(ExtraFields::class)->find($key);
89
90
            $extraData = $this->manager->getRepository(ExtraData::class)->findOneBy([
91
                'entity_id' => $entity_id,
92
                'entity_name' => $entity_name,
93
                'extra_fields' => $extra_fields
94
            ]);
95
96
            if (!$extraData)
97
                $extraData = new ExtraData();
98
99
            $extraData->setExtraFields($extra_fields);
100
            $extraData->setEntityName($entity_name);
101
            $extraData->setEntityId($entity_id);
102
            $extraData->setValue($data);
103
            $this->manager->persist($extraData);
104
        }
105
106
107
        $this->manager->flush();
108
    }
109
110
    public function  noChange()
111
    {
112
113
        $this->persistData();
114
    }
115
}
116