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\MongoDB; |
13
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Api\ItemDataProviderInterface; |
15
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
16
|
|
|
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; |
17
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
18
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
19
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
20
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
21
|
|
|
use Doctrine\ODM\MongoDB\DocumentRepository; |
22
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\QueryItemExtensionInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Item data provider for the Doctrine MongoDB ODM. |
26
|
|
|
*/ |
27
|
|
|
class ItemDataProvider implements ItemDataProviderInterface |
28
|
|
|
{ |
29
|
|
|
private $managerRegistry; |
30
|
|
|
private $propertyNameCollectionFactory; |
31
|
|
|
private $propertyMetadataFactory; |
32
|
|
|
private $itemExtensions; |
33
|
|
|
private $decorated; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ManagerRegistry $managerRegistry |
37
|
|
|
* @param PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory |
38
|
|
|
* @param PropertyMetadataFactoryInterface $propertyMetadataFactory |
39
|
|
|
* @param QueryItemExtensionInterface[] $itemExtensions |
40
|
|
|
* @param ItemDataProviderInterface|null $decorated |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $itemExtensions = [], ItemDataProviderInterface $decorated = null) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->managerRegistry = $managerRegistry; |
45
|
|
|
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
46
|
|
|
$this->propertyMetadataFactory = $propertyMetadataFactory; |
47
|
|
|
$this->itemExtensions = $itemExtensions; |
48
|
|
|
$this->decorated = $decorated; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function getItem(string $resourceClass, $id, string $operationName = null, bool $fetchData = false) |
55
|
|
|
{ |
56
|
|
|
if ($this->decorated) { |
57
|
|
|
try { |
58
|
|
|
return $this->decorated->getItem($resourceClass, $id, $operationName, $fetchData); |
59
|
|
|
} catch (ResourceClassNotSupportedException $resourceClassNotSupportedException) { |
60
|
|
|
// Ignore it |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$manager = $this->managerRegistry->getManagerForClass($resourceClass); |
65
|
|
|
if (null === $manager) { |
66
|
|
|
throw new ResourceClassNotSupportedException(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$identifierValues = explode('-', $id); |
70
|
|
|
$identifiers = []; |
71
|
|
|
$i = 0; |
72
|
|
|
|
73
|
|
View Code Duplication |
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
|
|
|
|
74
|
|
|
$itemMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
75
|
|
|
|
76
|
|
|
$identifier = $itemMetadata->isIdentifier(); |
77
|
|
|
if (null === $identifier || false === $identifier) { |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (!isset($identifierValues[$i])) { |
82
|
|
|
throw new InvalidArgumentException(sprintf('Invalid identifier "%s".', $id)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$identifiers[$propertyName] = $identifierValues[$i]; |
86
|
|
|
++$i; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!$fetchData || $manager instanceof DocumentManager) { |
|
|
|
|
90
|
|
|
return $manager->getReference($resourceClass, reset($identifiers)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** @var DocumentRepository $repository */ |
94
|
|
|
$repository = $manager->getRepository($resourceClass); |
95
|
|
|
$queryBuilder = $repository->createQueryBuilder(); |
96
|
|
|
|
97
|
|
|
foreach ($identifiers as $propertyName => $value) { |
98
|
|
|
|
99
|
|
|
$queryBuilder |
100
|
|
|
->field($propertyName)->equals($value) |
101
|
|
|
; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
foreach ($this->itemExtensions as $extension) { |
105
|
|
|
$extension->applyToItem($queryBuilder, $resourceClass, $identifiers, $operationName); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $queryBuilder->getQuery()->getSingleResult(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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.