ClassMetadataWrapper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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