Passed
Push — master ( f2e1d1...eea237 )
by Divine Niiquaye
03:30
created

IdReader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 80
ccs 0
cts 29
cp 0
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isSingleId() 0 3 1
A getIdField() 0 3 1
A getIdValue() 0 19 4
A isIntId() 0 3 1
A __construct() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\Database\Doctrine\Form\ChoiceList;
19
20
use Doctrine\Persistence\Mapping\ClassMetadata;
0 ignored issues
show
Bug introduced by
The type Doctrine\Persistence\Mapping\ClassMetadata 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...
21
use Doctrine\Persistence\ObjectManager;
0 ignored issues
show
Bug introduced by
The type Doctrine\Persistence\ObjectManager 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...
22
use Symfony\Component\Form\Exception\RuntimeException;
23
24
/**
25
 * A utility for reading object IDs.
26
 *
27
 * @author Bernhard Schussek <[email protected]>
28
 *
29
 * @internal
30
 */
31
class IdReader
32
{
33
    private ObjectManager $om;
34
    private ClassMetadata $classMetadata;
35
    private bool $singleId, $intId;
36
    private string $idField;
37
    private ?self $associationIdReader = null;
38
39
    public function __construct(ObjectManager $om, ClassMetadata $classMetadata)
40
    {
41
        $ids = $classMetadata->getIdentifierFieldNames();
42
        $idType = $classMetadata->getTypeOfField(\current($ids));
43
44
        $this->om = $om;
45
        $this->classMetadata = $classMetadata;
46
        $this->singleId = 1 === \count($ids);
47
        $this->intId = $this->singleId && \in_array($idType, ['integer', 'smallint', 'bigint']);
48
        $this->idField = \current($ids);
49
50
        // single field association are resolved, since the schema column could be an int
51
        if ($this->singleId && $classMetadata->hasAssociation($this->idField)) {
52
            $this->associationIdReader = new self($om, $om->getClassMetadata(
53
                $classMetadata->getAssociationTargetClass($this->idField)
54
            ));
55
56
            $this->singleId = $this->associationIdReader->isSingleId();
57
            $this->intId = $this->associationIdReader->isIntId();
58
        }
59
    }
60
61
    /**
62
     * Returns whether the class has a single-column ID.
63
     */
64
    public function isSingleId(): bool
65
    {
66
        return $this->singleId;
67
    }
68
69
    /**
70
     * Returns whether the class has a single-column integer ID.
71
     */
72
    public function isIntId(): bool
73
    {
74
        return $this->intId;
75
    }
76
77
    /**
78
     * Returns the ID value for an object.
79
     *
80
     * This method assumes that the object has a single-column ID.
81
     */
82
    public function getIdValue(object $object = null): string
83
    {
84
        if (!$object) {
85
            return '';
86
        }
87
88
        if (!$this->om->contains($object)) {
89
            throw new RuntimeException(\sprintf('Entity of type "%s" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?', \get_debug_type($object)));
90
        }
91
92
        $this->om->initializeObject($object);
93
94
        $idValue = \current($this->classMetadata->getIdentifierValues($object));
95
96
        if ($this->associationIdReader) {
97
            $idValue = $this->associationIdReader->getIdValue($idValue);
98
        }
99
100
        return (string) $idValue;
101
    }
102
103
    /**
104
     * Returns the name of the ID field.
105
     *
106
     * This method assumes that the object has a single-column ID.
107
     */
108
    public function getIdField(): string
109
    {
110
        return $this->idField;
111
    }
112
}
113