ClassMetadataAdapter::getClassName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Malef\Associate\DoctrineOrm\Metadata;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Doctrine\ORM\Mapping\MappingException;
9
use Doctrine\ORM\QueryBuilder;
10
11
class ClassMetadataAdapter
12
{
13
    /**
14
     * @var MetadataAdapterProvider
15
     */
16
    protected $metadataAdapterProvider;
17
18
    /**
19
     * @var EntityRepository
20
     */
21
    protected $repository;
22
23
    /**
24
     * @var ClassMetadata
25
     */
26
    protected $classMetadata;
27
28
    /**
29
     * @var string|null
30
     */
31
    protected $identifierFieldName;
32
33
    /**
34
     * @var AssociationMetadataAdapter[]
35
     */
36
    protected $associationMetadataAdapters = [];
37
38
    public function __construct(
39
        MetadataAdapterProvider $metadataAdapterProvider,
40
        EntityRepository $repository,
41
        ClassMetadata $classMetadata
42
    ) {
43
        $this->metadataAdapterProvider = $metadataAdapterProvider;
44
        $this->repository = $repository;
45
        $this->classMetadata = $classMetadata;
46
    }
47
48
    public function getClassName(): string
49
    {
50
        return $this->classMetadata->getName();
51
    }
52
53
    public function getRootClassName(): string
54
    {
55
        return $this->classMetadata->rootEntityName;
56
    }
57
58
    public function createQueryBuilder(string $rootAlias): QueryBuilder
59
    {
60
        return $this->repository->createQueryBuilder($rootAlias);
61
    }
62
63
    /**
64
     * @return mixed
65
     *
66
     * @throws \Exception
67
     */
68
    public function getIdentifierValueForOne(object $object)
69
    {
70
        $identifierValues = $this->classMetadata->getIdentifierValues($object);
71
72
        return $identifierValues[$this->getIdentifierFieldName()];
73
    }
74
75
    public function getIdentifierValueForMany(ArrayCollection $objects): ArrayCollection
76
    {
77
        $identifiers = new ArrayCollection();
78
        foreach ($objects->getValues() as $object) {
79
            $identifiers->add($this->getIdentifierValueForOne($object));
80
        }
81
82
        return $identifiers;
83
    }
84
85
    /**
86
     * @throws MappingException
87
     */
88
    public function getAssociationMetadataAdapter(string $associationName): ?AssociationMetadataAdapter
89
    {
90
        if (!array_key_exists($associationName, $this->associationMetadataAdapters)) {
91
            try {
92
                $associationMapping = $this->classMetadata->getAssociationMapping($associationName);
93
            } catch (MappingException $e) {
94
                if (0 !== strpos($e->getMessage(), 'No mapping found for field ')) {
95
                    throw $e;
96
                }
97
                $associationMapping = null;
98
            }
99
100
            $associationMetadataAdapter = is_null($associationMapping)
101
                ? null
102
                : new AssociationMetadataAdapter(
103
                    $this->metadataAdapterProvider,
104
                    $associationMapping
105
                );
106
107
            $this->associationMetadataAdapters[$associationName] = $associationMetadataAdapter;
108
        }
109
110
        return $this->associationMetadataAdapters[$associationName];
111
    }
112
113
    /**
114
     * @throws \Exception
115
     */
116
    public function getIdentifierFieldName(): string
117
    {
118
        if (is_null($this->identifierFieldName)) {
119
            $identifierFieldNames = $this->classMetadata->getIdentifierFieldNames();
120
121
            if (1 !== count($identifierFieldNames)) {
122
                throw new \Exception('Composite primary keys are not supported.');
123
            }
124
125
            $this->identifierFieldName = reset($identifierFieldNames);
126
        }
127
128
        return $this->identifierFieldName;
129
    }
130
}
131