Completed
Pull Request — master (#737)
by Antoine
07:17 queued 02:54
created

ItemDataProvider::getItem()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 29
Code Lines 16

Duplication

Lines 7
Ratio 24.14 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 29
rs 6.7272
cc 7
eloc 16
nc 5
nop 4
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ApiPlatform\Core\Bridge\Doctrine\Orm;
13
14
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
15
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultItemExtensionInterface;
16
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
17
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
18
use ApiPlatform\Core\Exception\PropertyNotFoundException;
19
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
20
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
22
use Doctrine\Common\Persistence\ManagerRegistry;
23
use Doctrine\Common\Persistence\ObjectManager;
24
use Doctrine\ORM\EntityManagerInterface;
25
use Doctrine\ORM\QueryBuilder;
26
27
/**
28
 * Item data provider for the Doctrine ORM.
29
 *
30
 * @author Kévin Dunglas <[email protected]>
31
 * @author Samuel ROZE <[email protected]>
32
 */
33
class ItemDataProvider implements ItemDataProviderInterface
34
{
35
    private $managerRegistry;
36
    private $propertyNameCollectionFactory;
37
    private $propertyMetadataFactory;
38
    private $itemExtensions;
39
40
    /**
41
     * @param ManagerRegistry                        $managerRegistry
42
     * @param PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory
43
     * @param PropertyMetadataFactoryInterface       $propertyMetadataFactory
44
     * @param QueryItemExtensionInterface[]          $itemExtensions
45
     */
46
    public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $itemExtensions = [])
47
    {
48
        $this->managerRegistry = $managerRegistry;
49
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
50
        $this->propertyMetadataFactory = $propertyMetadataFactory;
51
        $this->itemExtensions = $itemExtensions;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getItem(string $resourceClass, $id, string $operationName = null, bool $fetchData = false)
58
    {
59
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
60
        if (null === $manager) {
61
            throw new ResourceClassNotSupportedException();
62
        }
63
64
        $identifiers = $this->normalizeIdentifiers($id, $manager, $resourceClass);
65
66
        if (!$fetchData && $manager instanceof EntityManagerInterface) {
67
            return $manager->getReference($resourceClass, $identifiers);
68
        }
69
70
        $repository = $manager->getRepository($resourceClass);
71
        $queryBuilder = $repository->createQueryBuilder('o');
72
        $queryNameGenerator = new QueryNameGenerator();
73
74
        $this->addWhereForIdentifiers($identifiers, $queryBuilder);
75
76 View Code Duplication
        foreach ($this->itemExtensions as $extension) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
            $extension->applyToItem($queryBuilder, $queryNameGenerator, $resourceClass, $identifiers, $operationName);
78
79
            if ($extension instanceof QueryResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
80
                return $extension->getResult($queryBuilder);
81
            }
82
        }
83
84
        return $queryBuilder->getQuery()->getOneOrNullResult();
85
    }
86
87
    /**
88
     * Add WHERE conditions to the query for one or more identifiers (simple or composite).
89
     *
90
     * @param array        $identifiers
91
     * @param QueryBuilder $queryBuilder
92
     */
93
    private function addWhereForIdentifiers(array $identifiers, QueryBuilder $queryBuilder)
94
    {
95
        if (empty($identifiers)) {
96
            return;
97
        }
98
99
        foreach ($identifiers as $identifier => $value) {
100
            $placeholder = ':id_'.$identifier;
101
            $expression = $queryBuilder->expr()->eq(
102
                'o.'.$identifier,
103
                $placeholder
104
            );
105
106
            $queryBuilder->andWhere($expression);
107
108
            $queryBuilder->setParameter($placeholder, $value);
109
        }
110
    }
111
112
    /**
113
     * Transform and check the identifier, composite or not.
114
     *
115
     * @param int|string    $id
116
     * @param ObjectManager $manager
117
     * @param string        $resourceClass
118
     *
119
     * @return array
120
     */
121
    private function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass) : array
122
    {
123
        $identifierValues = [$id];
124
        $doctrineMetadataIdentifier = $manager->getClassMetadata($resourceClass)->getIdentifier();
125
126
        if (count($doctrineMetadataIdentifier) >= 2) {
127
            $identifiers = explode(';', $id);
128
            $identifiersMap = [];
129
130
            // first transform identifiers to a proper key/value array
131
            foreach ($identifiers as $identifier) {
132
                $keyValue = explode('=', $identifier);
133
                $identifiersMap[$keyValue[0]] = $keyValue[1];
134
            }
135
        }
136
137
        $identifiers = [];
138
        $i = 0;
139
140
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
141
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
142
143
            $identifier = $propertyMetadata->isIdentifier();
144
            if (null === $identifier || false === $identifier) {
145
                continue;
146
            }
147
148
            $identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null;
149
150
            if (null === $identifier) {
151
                throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName));
152
            }
153
154
            $identifiers[$propertyName] = $identifier;
155
            ++$i;
156
        }
157
158
        return $identifiers;
159
    }
160
}
161