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

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 hits per line

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

30.0
/src/Doctrine/Odm/Extension/ParameterExtension.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\Extension;
15

16
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
17
use ApiPlatform\Doctrine\Common\ParameterValueExtractorTrait;
18
use ApiPlatform\Doctrine\Odm\Filter\AbstractFilter;
19
use ApiPlatform\Doctrine\Odm\Filter\FilterInterface;
20
use ApiPlatform\Metadata\Operation;
21
use ApiPlatform\State\ParameterNotFound;
22
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
23
use Doctrine\ODM\MongoDB\Aggregation\Builder;
24
use Psr\Container\ContainerInterface;
25

26
/**
27
 * Reads operation parameters and execute its filter.
28
 *
29
 * @author Antoine Bluchet <soyuka@gmail.com>
30
 */
31
final class ParameterExtension implements AggregationCollectionExtensionInterface, AggregationItemExtensionInterface
32
{
33
    use ParameterValueExtractorTrait;
34

35
    public function __construct(
36
        private readonly ContainerInterface $filterLocator,
37
        private readonly ?ManagerRegistry $managerRegistry = null,
38
    ) {
39
    }
469✔
40

41
    /**
42
     * @param array<string, mixed> $context
43
     */
44
    private function applyFilter(Builder $aggregationBuilder, ?string $resourceClass = null, ?Operation $operation = null, array &$context = []): void
45
    {
UNCOV
46
        foreach ($operation->getParameters() ?? [] as $parameter) {
469✔
UNCOV
47
            if (null === ($v = $parameter->getValue()) || $v instanceof ParameterNotFound) {
220✔
48
                continue;
219✔
49
            }
50

51
            $values = $this->extractParameterValue($parameter, $v);
170✔
UNCOV
52
            if (null === ($filterId = $parameter->getFilter())) {
170✔
53
                continue;
170✔
54
            }
55

UNCOV
56
            $filter = match (true) {
×
UNCOV
57
                $filterId instanceof FilterInterface => $filterId,
×
UNCOV
58
                \is_string($filterId) && $this->filterLocator->has($filterId) => $this->filterLocator->get($filterId),
×
UNCOV
59
                default => null,
×
UNCOV
60
            };
×
61

UNCOV
62
            if (!$filter instanceof FilterInterface) {
×
UNCOV
63
                continue;
×
64
            }
65

UNCOV
66
            if ($this->managerRegistry && $filter instanceof ManagerRegistryAwareInterface && !$filter->hasManagerRegistry()) {
×
UNCOV
67
                $filter->setManagerRegistry($this->managerRegistry);
×
68
            }
69

UNCOV
70
            if ($filter instanceof AbstractFilter && !$filter->getProperties()) {
×
UNCOV
71
                $propertyKey = $parameter->getProperty() ?? $parameter->getKey();
×
72

73
                if (str_contains($propertyKey, ':property')) {
×
UNCOV
74
                    $extraProperties = $parameter->getExtraProperties()['_properties'] ?? [];
×
UNCOV
75
                    foreach (array_keys($extraProperties) as $property) {
×
UNCOV
76
                        $properties[$property] = $parameter->getFilterContext();
×
77
                    }
78
                } else {
UNCOV
79
                    $properties = [$propertyKey => $parameter->getFilterContext()];
×
80
                }
81

UNCOV
82
                $filter->setProperties($properties ?? []);
×
83
            }
84

UNCOV
85
            $filterContext = ['filters' => $values, 'parameter' => $parameter];
×
UNCOV
86
            $filter->apply($aggregationBuilder, $resourceClass, $operation, $filterContext);
×
87
            // update by reference
UNCOV
88
            if (isset($filterContext['mongodb_odm_sort_fields'])) {
×
UNCOV
89
                $context['mongodb_odm_sort_fields'] = $filterContext['mongodb_odm_sort_fields'];
×
90
            }
91
        }
92
    }
93

94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function applyToCollection(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
98
    {
UNCOV
99
        $this->applyFilter($aggregationBuilder, $resourceClass, $operation, $context);
274✔
100
    }
101

102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function applyToItem(Builder $aggregationBuilder, string $resourceClass, array $identifiers, ?Operation $operation = null, array &$context = []): void
106
    {
UNCOV
107
        $this->applyFilter($aggregationBuilder, $resourceClass, $operation, $context);
217✔
108
    }
109
}
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