• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10944992322

19 Sep 2024 04:21PM UTC coverage: 7.678% (+0.2%) from 7.518%
10944992322

push

github

soyuka
doc: changelog 4.0.1

12681 of 165161 relevant lines covered (7.68%)

16.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.15
/src/Doctrine/Odm/State/LinksHandlerTrait.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Doctrine\Odm\State;
15

16
use ApiPlatform\Doctrine\Common\State\LinksHandlerTrait as CommonLinksHandlerTrait;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\Link;
19
use ApiPlatform\Metadata\Operation;
20
use Doctrine\ODM\MongoDB\Aggregation\Builder;
21
use Doctrine\ODM\MongoDB\DocumentManager;
22
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
23
use Doctrine\Persistence\ManagerRegistry;
24

25
/**
26
 * @internal
27
 */
28
trait LinksHandlerTrait
29
{
30
    use CommonLinksHandlerTrait;
31

32
    private ManagerRegistry $managerRegistry;
33

34
    private function handleLinks(Builder $aggregationBuilder, array $identifiers, array $context, string $resourceClass, ?Operation $operation = null): void
35
    {
36
        if (!$identifiers) {
469✔
37
            return;
241✔
38
        }
39

40
        $links = $this->getLinks($resourceClass, $operation, $context);
241✔
41

42
        if (!$links) {
241✔
43
            return;
×
44
        }
45

46
        foreach ($links as $i => $link) {
241✔
47
            if (null !== $link->getExpandedValue()) {
241✔
48
                unset($links[$i]);
2✔
49
            }
50
        }
51

52
        $executeOptions = $operation->getExtraProperties()['doctrine_mongodb']['execute_options'] ?? [];
241✔
53

54
        $this->buildAggregation($resourceClass, array_reverse($links), array_reverse($identifiers), $context, $executeOptions, $resourceClass, $aggregationBuilder, $operation);
241✔
55
    }
56

57
    /**
58
     * @throws RuntimeException
59
     */
60
    private function buildAggregation(string $toClass, array $links, array $identifiers, array $context, array $executeOptions, string $previousAggregationClass, Builder $previousAggregationBuilder, ?Operation $operation = null): Builder
61
    {
62
        if (!$operation) {
241✔
63
            trigger_deprecation('api-platform/core', '3.2', 'In API Platform 4 the last argument "operation" will be required and this trait will be internal. Use the "handleLinks" feature instead.');
×
64
        }
65

66
        if (\count($links) <= 0) {
241✔
67
            return $previousAggregationBuilder;
241✔
68
        }
69

70
        /** @var Link $link */
71
        $link = array_shift($links);
241✔
72

73
        $fromClass = $link->getFromClass();
241✔
74
        $fromProperty = $link->getFromProperty();
241✔
75
        $toProperty = $link->getToProperty();
241✔
76
        $identifierProperties = $link->getIdentifiers();
241✔
77
        $hasCompositeIdentifiers = 1 < \count($identifierProperties);
241✔
78

79
        $aggregationClass = $fromClass;
241✔
80
        if ($toProperty) {
241✔
81
            $aggregationClass = $toClass;
22✔
82
        }
83

84
        $lookupProperty = $toProperty ?? $fromProperty;
241✔
85
        $lookupPropertyAlias = $lookupProperty ? "{$lookupProperty}_lkup" : null;
241✔
86

87
        $manager = $this->managerRegistry->getManagerForClass($aggregationClass);
241✔
88
        if (!$manager instanceof DocumentManager) {
241✔
89
            if ($operation) {
1✔
90
                $aggregationClass = $this->getLinkFromClass($link, $operation);
1✔
91
                $manager = $this->managerRegistry->getManagerForClass($aggregationClass);
1✔
92
            }
93

94
            if (!$manager instanceof DocumentManager) {
1✔
95
                throw new RuntimeException(\sprintf('The manager for "%s" must be an instance of "%s".', $aggregationClass, DocumentManager::class));
×
96
            }
97
        }
98

99
        $classMetadata = $manager->getClassMetadata($aggregationClass);
241✔
100

101
        if (!$classMetadata instanceof ClassMetadata) {
241✔
102
            throw new RuntimeException(\sprintf('The class metadata for "%s" must be an instance of "%s".', $aggregationClass, ClassMetadata::class));
×
103
        }
104

105
        $aggregation = $previousAggregationBuilder;
241✔
106
        if ($aggregationClass !== $previousAggregationClass) {
241✔
107
            $aggregation = $manager->createAggregationBuilder($aggregationClass);
34✔
108
        }
109

110
        if ($lookupProperty && $classMetadata->hasAssociation($lookupProperty)) {
241✔
111
            $aggregation->lookup($lookupProperty)->alias($lookupPropertyAlias);
56✔
112
        }
113

114
        if ($toProperty) {
241✔
115
            foreach ($identifierProperties as $identifierProperty) {
22✔
116
                $aggregation->match()->field(\sprintf('%s.%s', $lookupPropertyAlias, 'id' === $identifierProperty ? '_id' : $identifierProperty))->equals($this->getIdentifierValue($identifiers, $hasCompositeIdentifiers ? $identifierProperty : null));
22✔
117
            }
118
        } else {
119
            foreach ($identifierProperties as $identifierProperty) {
228✔
120
                $aggregation->match()->field($identifierProperty)->equals($this->getIdentifierValue($identifiers, $hasCompositeIdentifiers ? $identifierProperty : null));
228✔
121
            }
122
        }
123

124
        // Recurse aggregations
125
        $aggregation = $this->buildAggregation($fromClass, $links, $identifiers, $context, $executeOptions, $aggregationClass, $aggregation, $operation);
241✔
126

127
        if (null === $fromProperty || null !== $toProperty) {
241✔
128
            return $aggregation;
221✔
129
        }
130

131
        $results = $aggregation->execute($executeOptions)->toArray();
34✔
132
        $in = [];
34✔
133
        foreach ($results as $result) {
34✔
134
            foreach ($result[$lookupPropertyAlias] ?? [] as $lookupResult) {
33✔
135
                $in[] = $lookupResult['_id'];
32✔
136
            }
137
        }
138
        $previousAggregationBuilder->match()->field('_id')->in($in);
34✔
139

140
        return $previousAggregationBuilder;
34✔
141
    }
142

143
    private function getLinkFromClass(Link $link, Operation $operation): string
144
    {
145
        $fromClass = $link->getFromClass();
1✔
146
        if ($fromClass === $operation->getClass() && $documentClass = $this->getStateOptionsDocumentClass($operation)) {
1✔
147
            return $documentClass;
1✔
148
        }
149

150
        $operation = $this->resourceMetadataCollectionFactory->create($fromClass)->getOperation();
×
151

152
        if ($documentClass = $this->getStateOptionsDocumentClass($operation)) {
×
153
            return $documentClass;
×
154
        }
155

156
        throw new \Exception('Can not found a doctrine class for this link.');
×
157
    }
158

159
    private function getStateOptionsDocumentClass(Operation $operation): ?string
160
    {
161
        if (($options = $operation->getStateOptions()) && $options instanceof Options && $documentClass = $options->getDocumentClass()) {
1✔
162
            return $documentClass;
1✔
163
        }
164

165
        return null;
×
166
    }
167
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc