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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

78.26
/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 ApiPlatform\State\Util\StateOptionsTrait;
25
use Doctrine\ORM\EntityManagerInterface;
26
use Doctrine\Persistence\ManagerRegistry;
27
use Psr\Container\ContainerInterface;
28

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

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

51
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
52
    {
UNCOV
53
        $entityClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);
19✔
54

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

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

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

UNCOV
72
        $queryBuilder = $repository->createQueryBuilder('o');
19✔
UNCOV
73
        $queryNameGenerator = new QueryNameGenerator();
19✔
74

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

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

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

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