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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

85.0
/src/Doctrine/Odm/Filter/AbstractFilter.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\Filter;
15

16
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
17
use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface;
18
use ApiPlatform\Doctrine\Common\PropertyHelperTrait;
19
use ApiPlatform\Doctrine\Odm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait;
20
use ApiPlatform\Metadata\Exception\RuntimeException;
21
use ApiPlatform\Metadata\Operation;
22
use Doctrine\ODM\MongoDB\Aggregation\Builder;
23
use Doctrine\Persistence\ManagerRegistry;
24
use Psr\Log\LoggerInterface;
25
use Psr\Log\NullLogger;
26
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
27

28
/**
29
 * {@inheritdoc}
30
 *
31
 * Abstract class for easing the implementation of a filter.
32
 *
33
 * @author Alan Poulain <contact@alanpoulain.eu>
34
 */
35
abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface, ManagerRegistryAwareInterface
36
{
37
    use MongoDbOdmPropertyHelperTrait;
38
    use PropertyHelperTrait;
39
    protected LoggerInterface $logger;
40

41
    public function __construct(
42
        protected ?ManagerRegistry $managerRegistry = null,
43
        ?LoggerInterface $logger = null,
44
        protected ?array $properties = null,
45
        protected ?NameConverterInterface $nameConverter = null,
46
    ) {
47
        $this->logger = $logger ?? new NullLogger();
334✔
48
    }
49

50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
54
    {
UNCOV
55
        foreach ($context['filters'] as $property => $value) {
155✔
UNCOV
56
            $this->filterProperty($this->denormalizePropertyName($property), $value, $aggregationBuilder, $resourceClass, $operation, $context);
129✔
57
        }
58
    }
59

60
    /**
61
     * Passes a property through the filter.
62
     */
63
    abstract protected function filterProperty(string $property, $value, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void;
64

65
    public function hasManagerRegistry(): bool
66
    {
67
        return $this->managerRegistry instanceof ManagerRegistry;
260✔
68
    }
69

70
    public function getManagerRegistry(): ManagerRegistry
71
    {
72
        if (!$this->hasManagerRegistry()) {
260✔
UNCOV
73
            throw new RuntimeException('ManagerRegistry must be initialized before accessing it.');
1✔
74
        }
75

76
        return $this->managerRegistry;
260✔
77
    }
78

79
    public function setManagerRegistry(ManagerRegistry $managerRegistry): void
80
    {
81
        $this->managerRegistry = $managerRegistry;
×
82
    }
83

84
    /**
85
     * @return array<string, mixed>|null
86
     */
87
    public function getProperties(): ?array
88
    {
89
        return $this->properties;
247✔
90
    }
91

92
    /**
93
     * @param array<string, mixed> $properties
94
     */
95
    public function setProperties(array $properties): void
96
    {
UNCOV
97
        $this->properties = $properties;
145✔
98
    }
99

100
    protected function getLogger(): LoggerInterface
101
    {
UNCOV
102
        return $this->logger;
14✔
103
    }
104

105
    /**
106
     * Determines whether the given property is enabled.
107
     */
108
    protected function isPropertyEnabled(string $property, string $resourceClass): bool
109
    {
UNCOV
110
        if (null === $this->properties) {
129✔
111
            // to ensure sanity, nested properties must still be explicitly enabled
UNCOV
112
            return !$this->isPropertyNested($property, $resourceClass);
3✔
113
        }
114

UNCOV
115
        return \array_key_exists($property, $this->properties);
127✔
116
    }
117

118
    protected function denormalizePropertyName(string|int $property): string
119
    {
UNCOV
120
        if (!$this->nameConverter instanceof NameConverterInterface) {
131✔
121
            return (string) $property;
×
122
        }
123

UNCOV
124
        return implode('.', array_map($this->nameConverter->denormalize(...), explode('.', (string) $property)));
131✔
125
    }
126

127
    protected function normalizePropertyName(string $property): string
128
    {
UNCOV
129
        if (!$this->nameConverter instanceof NameConverterInterface) {
245✔
130
            return $property;
×
131
        }
132

UNCOV
133
        return implode('.', array_map($this->nameConverter->normalize(...), explode('.', $property)));
245✔
134
    }
135
}
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