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

api-platform / core / 10739011304

06 Sep 2024 01:11PM UTC coverage: 7.159% (-0.5%) from 7.645%
10739011304

push

github

web-flow
 feat(laravel): eloquent filters (search, date, equals, or) (#6593)

* feat(laravel): Eloquent filters search date or

* feat(laravel): eloquent filters order range equals afterdate

* fix(laravel): order afterDate filters

* temp

* test(laravel): filter with eloquent

---------

Co-authored-by: Nathan <nathan@les-tilleuls.coop>

0 of 144 new or added lines in 16 files covered. (0.0%)

4792 existing lines in 155 files now uncovered.

11736 of 163930 relevant lines covered (7.16%)

22.8 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
    {
UNCOV
36
        if (!$identifiers) {
936✔
UNCOV
37
            return;
482✔
38
        }
39

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

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

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

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

UNCOV
54
        $this->buildAggregation($resourceClass, array_reverse($links), array_reverse($identifiers), $context, $executeOptions, $resourceClass, $aggregationBuilder, $operation);
480✔
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
    {
UNCOV
62
        if (!$operation) {
480✔
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

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

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

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

UNCOV
79
        $aggregationClass = $fromClass;
480✔
UNCOV
80
        if ($toProperty) {
480✔
UNCOV
81
            $aggregationClass = $toClass;
44✔
82
        }
83

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

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

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

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

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

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

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

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

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

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

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

UNCOV
140
        return $previousAggregationBuilder;
68✔
141
    }
142

143
    private function getLinkFromClass(Link $link, Operation $operation): string
144
    {
UNCOV
145
        $fromClass = $link->getFromClass();
2✔
UNCOV
146
        if ($fromClass === $operation->getClass() && $documentClass = $this->getStateOptionsDocumentClass($operation)) {
2✔
UNCOV
147
            return $documentClass;
2✔
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
    {
UNCOV
161
        if (($options = $operation->getStateOptions()) && $options instanceof Options && $documentClass = $options->getDocumentClass()) {
2✔
UNCOV
162
            return $documentClass;
2✔
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

© 2024 Coveralls, Inc