Passed
Push — master ( f8faaa...aedd7e )
by Luiz Kim
02:31
created

ConfigService::discoveryConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 14
rs 9.9332
cc 3
nc 3
nop 3
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Config;
6
use ControleOnline\Entity\People;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\People 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...
7
use ControleOnline\Entity\PeopleDomain;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\PeopleDomain 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\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...
9
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request 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
use InvalidArgumentException;
11
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...
12
use phpDocumentor\Reflection\PseudoTypes\Numeric_;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\PseudoTypes\Numeric_ 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
use phpDocumentor\Reflection\Types\Integer;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\Types\Integer 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...
14
15
class ConfigService
16
{
17
    private $request;
18
    public function __construct(
19
        private EntityManagerInterface $manager,
20
        private RequestStack $requestStack
21
    ) {
22
        $this->request = $requestStack->getCurrentRequest();
23
    }
24
25
    public function getConfig(People $people, $key, $json = false)
26
    {
27
        $config = $this->discoveryConfig($people, $key, false);
28
        $value =  $config ? $config->getConfigValue() : null;
29
        return $json ? json_decode($value) : $value;
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        return $json ? json_decode(/** @scrutinizer ignore-type */ $value) : $value;
Loading history...
30
    }
31
32
    private function discoveryConfig(People $people, $key, $create = true): ?Config
33
    {
34
        $config =   $this->manager->getRepository(Config::class)->findOneBy([
35
            'people' => $people,
36
            'configKey' => $key
37
        ]);
38
        if ($config)
39
            return $config;
40
        if ($create) {
41
            $config = new Config();
42
            $config->setConfigKey($key);
43
            $config->setPeople($people);
44
        }
45
        return null;
46
    }
47
48
    public function addConfig(People $people, string $key, array $values)
49
    {
50
        $config = $this->discoveryConfig($people, $key);
51
        $newValue = json_decode($config->getConfigValue()) || [];
52
        if (!is_array($newValue))
0 ignored issues
show
introduced by
The condition is_array($newValue) is always false.
Loading history...
53
            $newValue = [$newValue];
54
55
        if (is_array($values))
0 ignored issues
show
introduced by
The condition is_array($values) is always true.
Loading history...
56
            foreach ($values as $key => $value)
57
                $newValue[$key] = $value;
58
        else
59
            $newValue[] = $values;
60
61
        $config->setConfigValue(json_encode($newValue));
62
        $this->manager->persist($config);
63
        $this->manager->flush();
64
        return $config;
65
    }
66
}
67