Passed
Push — master ( 294dbb...019655 )
by Luiz Kim
02:13
created

ExtraDataService::persistData()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 20
c 4
b 0
f 0
dl 0
loc 32
rs 8.6666
cc 7
nc 9
nop 2
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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Security\Core\Security 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...
8
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...
9
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...
10
11
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
15
class ExtraDataService
16
{
17
    private static $persisted = false;
18
    private $request;
19
    public function __construct(
20
        private  EntityManagerInterface $manager,
21
        RequestStack $requestStack
22
    ) {
23
        $this->request = $requestStack->getCurrentRequest();
24
    }
25
    public function persist($entity)
26
    {
27
        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...
28
            return;
29
        self::$persisted = true;
30
31
        $json =       json_decode($this->request->getContent(), true);
32
        $extra_data = isset($json['extra-data']) ? $json['extra-data'] : null;
33
34
        if (!$extra_data)
35
            return;
36
37
        //$this->manager->persist($entity);
38
        //$this->manager->flush();
39
        $this->persistData(
40
            $entity->getId(),
41
            (new \ReflectionClass($entity::class))->getShortName()
42
        );
43
    }
44
    private function persistData($entity_id, $entity_name)
45
    {
46
        if (!$entity_id || !$entity_name)
47
            return;
48
        $json =       json_decode($this->request->getContent(), true);
49
        $extra_data = isset($json['extra-data']) ? $json['extra-data'] : null;
50
51
        if (!$extra_data)
52
            return;
53
54
55
        foreach ($extra_data['data'] as $key => $data) {
56
            $extra_fields = $this->manager->getRepository(ExtraFields::class)->find($key);
57
58
            $extraData = $this->manager->getRepository(ExtraData::class)->findOneBy([
59
                'entity_id' => $entity_id,
60
                'entity_name' => $entity_name,
61
                'extra_fields' => $extra_fields
62
            ]);
63
64
            if (!$extraData)
65
                $extraData = new ExtraData();
66
67
            $extraData->setExtraFields($extra_fields);
68
            $extraData->setEntityName($entity_name);
69
            $extraData->setEntityId($entity_id);
70
            $extraData->setValue($data);
71
            $this->manager->persist($extraData);
72
        }
73
74
75
        $this->manager->flush();
76
    }
77
78
    public function  noChange()
79
    {
80
        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...
81
            return;
82
        self::$persisted = true;
83
        $json =       json_decode($this->request->getContent(), true);
84
        $extra_data = isset($json['extra-data']) ? $json['extra-data'] : null;
85
        if (!$extra_data)
86
            return;
87
88
        $this->persistData($extra_data['entity_id'], $extra_data['entity_name']);
89
    }
90
}
91