Passed
Pull Request — master (#13)
by De Cramer
07:26
created

EntityConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
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 18
rs 10
cc 1
nc 1
nop 11

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
4
namespace oliverde8\ComfyBundle\Model;
5
6
7
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...
8
use oliverde8\ComfyBundle\Manager\ConfigManagerInterface;
9
use Symfony\Component\Validator\Validator\ValidatorInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Valida...ator\ValidatorInterface 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
class EntityConfig extends TextConfig
12
{
13
    private EntityManagerInterface $entityManager;
14
15
    protected string $entity;
16
17
    protected string $choiceLabel;
18
19
    public function __construct(
20
        ConfigManagerInterface $configManager,
21
        ValidatorInterface $validator,
22
        EntityManagerInterface $entityManager,
23
        string $path,
24
        string $name,
25
        string $description = "",
26
        int $scope = PHP_INT_MAX,
27
        ?string $defaultValue = null,
28
        bool $isHidden = false,
29
        string $entity = "",
30
        string $choiceLabel = "id"
31
    ) {
32
        parent::__construct($configManager, $validator, $path, $name, $description, $scope, $defaultValue, $isHidden);
33
34
        $this->entityManager = $entityManager;
35
        $this->entity = $entity;
36
        $this->choiceLabel = $choiceLabel;
37
    }
38
39
    /**
40
     * Serialize value to save it in Database.
41
     *
42
     * @param $value
43
     *
44
     * @return string|null
45
     */
46
    protected function serialize($value): ?string
47
    {
48
        if (is_object($value)) {
49
            return $value->getId();
50
        } else {
51
            return $value;
52
        }
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getEntity(): string
59
    {
60
        return $this->entity;
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getChoiceLabel(): string
67
    {
68
        return $this->choiceLabel;
69
    }
70
71
    /**
72
     * Deserialize value read from database.
73
     *
74
     * @param string|null $value
75
     *
76
     * @return mixed|null
77
     */
78
    protected function deserialize(?string $value)
79
    {
80
        if ($value) {
81
            return $this->entityManager->getRepository($this->entity)->find($value);
82
        } else {
83
            return null;
84
        }
85
    }
86
}
87