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

api-platform / core / 13724363992

07 Mar 2025 03:55PM UTC coverage: 8.174% (-0.3%) from 8.518%
13724363992

Pull #7004

github

web-flow
Merge c88b49ea9 into 1e0bc9dc8
Pull Request #7004: fix(doctrine): correct the use statement for ManagerRegistry

12837 of 157041 relevant lines covered (8.17%)

13.55 hits per line

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

100.0
/src/Doctrine/Orm/Metadata/Resource/DoctrineOrmResourceCollectionMetadataFactory.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\Metadata\Resource;
15

16
use ApiPlatform\Doctrine\Orm\State\CollectionProvider;
17
use ApiPlatform\Doctrine\Orm\State\ItemProvider;
18
use ApiPlatform\Doctrine\Orm\State\Options;
19
use ApiPlatform\Metadata\ApiResource;
20
use ApiPlatform\Metadata\CollectionOperationInterface;
21
use ApiPlatform\Metadata\DeleteOperationInterface;
22
use ApiPlatform\Metadata\Operation;
23
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
24
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
25
use Doctrine\ORM\EntityManagerInterface;
26
use Doctrine\Persistence\ManagerRegistry;
27

28
final class DoctrineOrmResourceCollectionMetadataFactory implements ResourceMetadataCollectionFactoryInterface
29
{
30
    public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly ResourceMetadataCollectionFactoryInterface $decorated)
31
    {
32
    }
1,339✔
33

34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function create(string $resourceClass): ResourceMetadataCollection
38
    {
39
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
106✔
40

41
        /** @var ApiResource $resourceMetadata */
42
        foreach ($resourceMetadataCollection as $i => $resourceMetadata) {
106✔
43
            $operations = $resourceMetadata->getOperations();
102✔
44

45
            if ($operations) {
102✔
46
                /** @var Operation $operation */
47
                foreach ($resourceMetadata->getOperations() as $operationName => $operation) {
102✔
48
                    $entityClass = $operation->getClass();
102✔
49
                    if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
102✔
50
                        $entityClass = $options->getEntityClass();
8✔
51
                    }
52

53
                    if (!$this->managerRegistry->getManagerForClass($entityClass) instanceof EntityManagerInterface) {
102✔
54
                        continue;
51✔
55
                    }
56

57
                    $operations->add($operationName, $this->addDefaults($operation));
64✔
58
                }
59

60
                $resourceMetadata = $resourceMetadata->withOperations($operations);
102✔
61
            }
62

63
            $graphQlOperations = $resourceMetadata->getGraphQlOperations();
102✔
64

65
            if ($graphQlOperations) {
102✔
66
                foreach ($graphQlOperations as $operationName => $graphQlOperation) {
85✔
67
                    $entityClass = $graphQlOperation->getClass();
85✔
68
                    if (($options = $graphQlOperation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
85✔
69
                        $entityClass = $options->getEntityClass();
4✔
70
                    }
71

72
                    if (!$this->managerRegistry->getManagerForClass($entityClass) instanceof EntityManagerInterface) {
85✔
73
                        continue;
29✔
74
                    }
75

76
                    $graphQlOperations[$operationName] = $this->addDefaults($graphQlOperation);
62✔
77
                }
78

79
                $resourceMetadata = $resourceMetadata->withGraphQlOperations($graphQlOperations);
85✔
80
            }
81

82
            $resourceMetadataCollection[$i] = $resourceMetadata;
102✔
83
        }
84

85
        return $resourceMetadataCollection;
106✔
86
    }
87

88
    private function addDefaults(Operation $operation): Operation
89
    {
90
        if (null === $operation->getProvider()) {
64✔
91
            $operation = $operation->withProvider($this->getProvider($operation));
64✔
92
        }
93

94
        $options = $operation->getStateOptions() ?: new Options();
64✔
95
        if ($options instanceof Options && null === $options->getHandleLinks()) {
64✔
96
            $options = $options->withHandleLinks('api_platform.doctrine.orm.links_handler');
63✔
97
            $operation = $operation->withStateOptions($options);
63✔
98
        }
99

100
        if (null === $operation->getProcessor()) {
64✔
101
            $operation = $operation->withProcessor($this->getProcessor($operation));
64✔
102
        }
103

104
        return $operation;
64✔
105
    }
106

107
    private function getProvider(Operation $operation): string
108
    {
109
        if ($operation instanceof CollectionOperationInterface) {
64✔
110
            return CollectionProvider::class;
63✔
111
        }
112

113
        return ItemProvider::class;
63✔
114
    }
115

116
    private function getProcessor(Operation $operation): string
117
    {
118
        if ($operation instanceof DeleteOperationInterface) {
64✔
119
            return 'api_platform.doctrine.orm.state.remove_processor';
56✔
120
        }
121

122
        return 'api_platform.doctrine.orm.state.persist_processor';
64✔
123
    }
124
}
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