Passed
Push — master ( 8bd912...d93388 )
by Alan
06:58 queued 02:20
created

Doctrine/MongoDbOdm/SubresourceDataProvider.php (1 issue)

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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Common\Util\IdentifierManagerTrait;
17
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationCollectionExtensionInterface;
18
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationItemExtensionInterface;
19
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationResultCollectionExtensionInterface;
20
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationResultItemExtensionInterface;
21
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
22
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
23
use ApiPlatform\Core\Exception\RuntimeException;
24
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
25
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
26
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
27
use Doctrine\Common\Persistence\ManagerRegistry;
28
use Doctrine\ODM\MongoDB\Aggregation\Builder;
29
use Doctrine\ODM\MongoDB\DocumentManager;
30
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
31
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
32
33
/**
34
 * Subresource data provider for the Doctrine MongoDB ODM.
35
 *
36
 * @experimental
37
 *
38
 * @author Antoine Bluchet <[email protected]>
39
 * @author Alan Poulain <[email protected]>
40
 */
41
final class SubresourceDataProvider implements SubresourceDataProviderInterface
42
{
43
    use IdentifierManagerTrait;
44
45
    private $managerRegistry;
46
    private $collectionExtensions;
47
    private $itemExtensions;
48
49
    /**
50
     * @param AggregationCollectionExtensionInterface[] $collectionExtensions
51
     * @param AggregationItemExtensionInterface[]       $itemExtensions
52
     */
53
    public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, iterable $collectionExtensions = [], iterable $itemExtensions = [])
54
    {
55
        $this->managerRegistry = $managerRegistry;
56
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
57
        $this->propertyMetadataFactory = $propertyMetadataFactory;
58
        $this->collectionExtensions = $collectionExtensions;
59
        $this->itemExtensions = $itemExtensions;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     *
65
     * @throws RuntimeException
66
     */
67
    public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null)
68
    {
69
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
70
        if (!$manager instanceof DocumentManager) {
71
            throw new ResourceClassNotSupportedException(sprintf('The manager for "%s" must be an instance of "%s".', $resourceClass, DocumentManager::class));
72
        }
73
74
        $repository = $manager->getRepository($resourceClass);
75
        if (!$repository instanceof DocumentRepository) {
76
            throw new RuntimeException(sprintf('The repository for "%s" must be an instance of "%s".', $resourceClass, DocumentRepository::class));
77
        }
78
79
        if (!isset($context['identifiers'], $context['property'])) {
80
            throw new ResourceClassNotSupportedException('The given resource class is not a subresource.');
81
        }
82
83
        $aggregationBuilder = $this->buildAggregation($identifiers, $context, $repository->createAggregationBuilder(), \count($context['identifiers']));
84
85
        if (true === $context['collection']) {
86
            foreach ($this->collectionExtensions as $extension) {
87
                $extension->applyToCollection($aggregationBuilder, $resourceClass, $operationName, $context);
88
                if ($extension instanceof AggregationResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) {
89
                    return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context);
90
                }
91
            }
92
        } else {
93
            foreach ($this->itemExtensions as $extension) {
94
                $extension->applyToItem($aggregationBuilder, $resourceClass, $identifiers, $operationName, $context);
95
                if ($extension instanceof AggregationResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) {
96
                    return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context);
97
                }
98
            }
99
        }
100
101
        $iterator = $aggregationBuilder->hydrate($resourceClass)->execute();
102
103
        return $context['collection'] ? $iterator->toArray() : ($iterator->current() ?: null);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $context['collect...ator->current() ?: null also could return the type array which is incompatible with the return type mandated by ApiPlatform\Core\DataPro...rface::getSubresource() of iterable|null|object.
Loading history...
104
    }
105
106
    /**
107
     * @throws RuntimeException
108
     */
109
    private function buildAggregation(array $identifiers, array $context, Builder $previousAggregationBuilder, int $remainingIdentifiers, Builder $topAggregationBuilder = null): Builder
110
    {
111
        if ($remainingIdentifiers <= 0) {
112
            return $previousAggregationBuilder;
113
        }
114
115
        $topAggregationBuilder = $topAggregationBuilder ?? $previousAggregationBuilder;
116
117
        [$identifier, $identifierResourceClass] = $context['identifiers'][$remainingIdentifiers - 1];
118
        $previousAssociationProperty = $context['identifiers'][$remainingIdentifiers][0] ?? $context['property'];
119
120
        $manager = $this->managerRegistry->getManagerForClass($identifierResourceClass);
121
        if (!$manager instanceof DocumentManager) {
122
            throw new RuntimeException(sprintf('The manager for "%s" must be an instance of "%s".', $identifierResourceClass, DocumentManager::class));
123
        }
124
125
        $classMetadata = $manager->getClassMetadata($identifierResourceClass);
126
127
        if (!$classMetadata instanceof ClassMetadata) {
128
            throw new RuntimeException(sprintf('The class metadata for "%s" must be an instance of "%s".', $identifierResourceClass, ClassMetadata::class));
129
        }
130
131
        $aggregation = $manager->createAggregationBuilder($identifierResourceClass);
132
        $normalizedIdentifiers = [];
133
134
        if (isset($identifiers[$identifier])) {
135
            // if it's an array it's already normalized, the IdentifierManagerTrait is deprecated
136
            if ($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false) {
137
                $normalizedIdentifiers = $identifiers[$identifier];
138
            } else {
139
                $normalizedIdentifiers = $this->normalizeIdentifiers($identifiers[$identifier], $manager, $identifierResourceClass);
140
            }
141
        }
142
143
        if ($classMetadata->hasAssociation($previousAssociationProperty)) {
144
            $aggregation->lookup($previousAssociationProperty)->alias($previousAssociationProperty);
145
            foreach ($normalizedIdentifiers as $key => $value) {
146
                $aggregation->match()->field($key)->equals($value);
147
            }
148
        } elseif ($classMetadata->isIdentifier($previousAssociationProperty)) {
149
            foreach ($normalizedIdentifiers as $key => $value) {
150
                $aggregation->match()->field($key)->equals($value);
151
            }
152
153
            return $aggregation;
154
        }
155
156
        // Recurse aggregations
157
        $aggregation = $this->buildAggregation($identifiers, $context, $aggregation, --$remainingIdentifiers, $topAggregationBuilder);
158
159
        $results = $aggregation->execute()->toArray();
160
        $in = array_reduce($results, function ($in, $result) use ($previousAssociationProperty) {
161
            return $in + array_map(function ($result) {
162
                return $result['_id'];
163
            }, $result[$previousAssociationProperty] ?? []);
164
        }, []);
165
        $previousAggregationBuilder->match()->field('_id')->in($in);
166
167
        return $previousAggregationBuilder;
168
    }
169
}
170