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

api-platform / core / 13814792797

12 Mar 2025 03:09PM UTC coverage: 5.889% (-1.4%) from 7.289%
13814792797

Pull #7012

github

web-flow
Merge 199d44919 into 284937039
Pull Request #7012: doc: comment typo in ApiResource.php

10048 of 170615 relevant lines covered (5.89%)

5.17 hits per line

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

0.0
/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) {
×
37
            return;
×
38
        }
39

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

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

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

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

54
        $this->buildAggregation($resourceClass, array_reverse($links), array_reverse($identifiers), $context, $executeOptions, $resourceClass, $aggregationBuilder, $operation);
×
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) {
×
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) {
×
67
            return $previousAggregationBuilder;
×
68
        }
69

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

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

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

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

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

94
            if (!$manager instanceof DocumentManager) {
×
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);
×
100

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

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

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

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

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

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

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

140
        return $previousAggregationBuilder;
×
141
    }
142

143
    private function getLinkFromClass(Link $link, Operation $operation): string
144
    {
145
        $fromClass = $link->getFromClass();
×
146
        if ($fromClass === $operation->getClass() && $documentClass = $this->getStateOptionsDocumentClass($operation)) {
×
147
            return $documentClass;
×
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()) {
×
162
            return $documentClass;
×
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

© 2025 Coveralls, Inc