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

api-platform / core / 14008635868

22 Mar 2025 12:39PM UTC coverage: 8.52% (+0.005%) from 8.515%
14008635868

Pull #7042

github

web-flow
Merge fdd88ef56 into 47a6dffbb
Pull Request #7042: Purge parent collections in inheritance cases

4 of 9 new or added lines in 1 file covered. (44.44%)

540 existing lines in 57 files now uncovered.

13394 of 157210 relevant lines covered (8.52%)

22.93 hits per line

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

84.0
/src/Doctrine/Orm/State/ItemProvider.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\Orm\State;
15

16
use ApiPlatform\Doctrine\Common\State\LinksHandlerLocatorTrait;
17
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
18
use ApiPlatform\Doctrine\Orm\Extension\QueryResultItemExtensionInterface;
19
use ApiPlatform\Doctrine\Orm\Util\QueryNameGenerator;
20
use ApiPlatform\Metadata\Exception\RuntimeException;
21
use ApiPlatform\Metadata\Operation;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\State\ProviderInterface;
24
use Doctrine\ORM\EntityManagerInterface;
25
use Doctrine\Persistence\ManagerRegistry;
26
use Psr\Container\ContainerInterface;
27

28
/**
29
 * Item state provider using the Doctrine ORM.
30
 *
31
 * @author Kévin Dunglas <kevin@dunglas.fr>
32
 * @author Samuel ROZE <samuel.roze@gmail.com>
33
 */
34
final class ItemProvider implements ProviderInterface
35
{
36
    use LinksHandlerLocatorTrait;
37
    use LinksHandlerTrait;
38

39
    /**
40
     * @param QueryItemExtensionInterface[] $itemExtensions
41
     */
42
    public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, ManagerRegistry $managerRegistry, private readonly iterable $itemExtensions = [], ?ContainerInterface $handleLinksLocator = null)
43
    {
44
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
309✔
45
        $this->handleLinksLocator = $handleLinksLocator;
309✔
46
        $this->managerRegistry = $managerRegistry;
309✔
47
    }
48

49
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
50
    {
51
        $entityClass = $operation->getClass();
303✔
52
        if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
303✔
UNCOV
53
            $entityClass = $options->getEntityClass();
4✔
54
        }
55

56
        /** @var EntityManagerInterface|null $manager */
57
        $manager = $this->managerRegistry->getManagerForClass($entityClass);
303✔
58
        if (null === $manager) {
303✔
59
            throw new RuntimeException(\sprintf('No manager found for class "%s". Are you sure it\'s an entity?', $entityClass));
×
60
        }
61

62
        $fetchData = $context['fetch_data'] ?? true;
303✔
63
        if (!$fetchData && \array_key_exists('id', $uriVariables)) {
303✔
64
            // todo : if uriVariables don't contain the id, this fails. This should behave like it does in the following code
UNCOV
65
            return $manager->getReference($entityClass, $uriVariables);
4✔
66
        }
67

68
        $repository = $manager->getRepository($entityClass);
299✔
69
        if (!method_exists($repository, 'createQueryBuilder')) {
299✔
70
            throw new RuntimeException('The repository class must have a "createQueryBuilder" method.');
×
71
        }
72

73
        $queryBuilder = $repository->createQueryBuilder('o');
299✔
74
        $queryNameGenerator = new QueryNameGenerator();
299✔
75

76
        if ($handleLinks = $this->getLinksHandler($operation)) {
299✔
77
            $handleLinks($queryBuilder, $uriVariables, $queryNameGenerator, ['entityClass' => $entityClass, 'operation' => $operation] + $context);
299✔
78
        } else {
79
            $this->handleLinks($queryBuilder, $uriVariables, $queryNameGenerator, $context, $entityClass, $operation);
×
80
        }
81

82
        foreach ($this->itemExtensions as $extension) {
299✔
83
            $extension->applyToItem($queryBuilder, $queryNameGenerator, $entityClass, $uriVariables, $operation, $context);
299✔
84

85
            if ($extension instanceof QueryResultItemExtensionInterface && $extension->supportsResult($entityClass, $operation, $context)) {
299✔
86
                return $extension->getResult($queryBuilder, $entityClass, $operation, $context);
×
87
            }
88
        }
89

90
        return $queryBuilder->getQuery()->getOneOrNullResult();
299✔
91
    }
92
}
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