Passed
Push — master ( 5b5bb9...d21849 )
by Luiz Kim
02:43
created

ExtraDataService::noChange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 0
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
        $this->persistData(
28
            $entity->getId(),
29
            (new \ReflectionClass($entity::class))->getShortName()
30
        );
31
    }
32
    public function persistData($entity_id, $entity_name)
33
    {
34
35
        if (self::$persisted == true || !$entity_id || !$entity_name)
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...
36
            return;
37
38
        $extra_data = json_decode($this->request->getContent(), true)['extra-data'] ?? null;
39
        if (!$extra_data)
40
            return;
41
42
43
        foreach ($extra_data['data'] as $key => $data) {
44
            $extra_fields = $this->manager->getRepository(ExtraFields::class)->find($key);
45
46
            $extraData = $this->manager->getRepository(ExtraData::class)->findOneBy([
47
                'entity_id' => $entity_id,
48
                'entity_name' => $entity_name,
49
                'extra_fields' => $extra_fields
50
            ]);
51
52
            if (!$extraData)
53
                $extraData = new ExtraData();
54
55
            $extraData->setExtraFields($extra_fields);
56
            $extraData->setEntityName($entity_name);
57
            $extraData->setEntityId($entity_id);
58
            $extraData->setValue($data);
59
            $this->manager->persist($extraData);
60
        }
61
62
        self::$persisted = true;
63
        $this->manager->flush();
64
    }
65
66
    public function  noChange()
67
    {
68
        $extra_data = json_decode($this->request->getContent(), true)['extra-data'] ?? null;
69
        if (!$extra_data)
70
            return;
71
72
        $this->persistData($extra_data['entity_id'], $extra_data['entity_name']);
73
    }
74
}
75