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

api-platform / core / 15955054194

29 Jun 2025 12:10PM UTC coverage: 22.026% (+0.2%) from 21.793%
15955054194

push

github

soyuka
cs: unused namespace

11500 of 52210 relevant lines covered (22.03%)

21.66 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\CollectionOperationInterface;
20
use ApiPlatform\Metadata\DeleteOperationInterface;
21
use ApiPlatform\Metadata\Operation;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
24
use ApiPlatform\State\Util\StateOptionsTrait;
25
use Doctrine\ORM\EntityManagerInterface;
26
use Doctrine\Persistence\ManagerRegistry;
27

28
final class DoctrineOrmResourceCollectionMetadataFactory implements ResourceMetadataCollectionFactoryInterface
29
{
30
    use StateOptionsTrait;
31

32
    public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly ResourceMetadataCollectionFactoryInterface $decorated)
33
    {
34
    }
580✔
35

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

43
        foreach ($resourceMetadataCollection as $i => $resourceMetadata) {
98✔
44
            $operations = $resourceMetadata->getOperations();
96✔
45

46
            if ($operations) {
96✔
47
                foreach ($resourceMetadata->getOperations() as $operationName => $operation) {
96✔
48
                    $entityClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);
96✔
49

50
                    if (!$this->managerRegistry->getManagerForClass($entityClass) instanceof EntityManagerInterface) {
96✔
51
                        continue;
56✔
52
                    }
53

54
                    $operations->add($operationName, $this->addDefaults($operation));
48✔
55
                }
56

57
                $resourceMetadata = $resourceMetadata->withOperations($operations);
96✔
58
            }
59

60
            $graphQlOperations = $resourceMetadata->getGraphQlOperations();
96✔
61

62
            if ($graphQlOperations) {
96✔
63
                foreach ($graphQlOperations as $operationName => $graphQlOperation) {
70✔
64
                    $entityClass = $this->getStateOptionsClass($graphQlOperation, $graphQlOperation->getClass(), Options::class);
70✔
65

66
                    if (!$this->managerRegistry->getManagerForClass($entityClass) instanceof EntityManagerInterface) {
70✔
67
                        continue;
30✔
68
                    }
69

70
                    $graphQlOperations[$operationName] = $this->addDefaults($graphQlOperation);
44✔
71
                }
72

73
                $resourceMetadata = $resourceMetadata->withGraphQlOperations($graphQlOperations);
70✔
74
            }
75

76
            $resourceMetadataCollection[$i] = $resourceMetadata;
96✔
77
        }
78

79
        return $resourceMetadataCollection;
98✔
80
    }
81

82
    private function addDefaults(Operation $operation): Operation
83
    {
84
        if (null === $operation->getProvider()) {
48✔
85
            $operation = $operation->withProvider($this->getProvider($operation));
48✔
86
        }
87

88
        $options = $operation->getStateOptions() ?: new Options();
48✔
89
        if ($options instanceof Options && null === $options->getHandleLinks()) {
48✔
90
            $options = $options->withHandleLinks('api_platform.doctrine.orm.links_handler');
48✔
91
            $operation = $operation->withStateOptions($options);
48✔
92
        }
93

94
        if (null === $operation->getProcessor()) {
48✔
95
            $operation = $operation->withProcessor($this->getProcessor($operation));
48✔
96
        }
97

98
        return $operation;
48✔
99
    }
100

101
    private function getProvider(Operation $operation): string
102
    {
103
        if ($operation instanceof CollectionOperationInterface) {
48✔
104
            return CollectionProvider::class;
48✔
105
        }
106

107
        return ItemProvider::class;
48✔
108
    }
109

110
    private function getProcessor(Operation $operation): string
111
    {
112
        if ($operation instanceof DeleteOperationInterface) {
48✔
113
            return 'api_platform.doctrine.orm.state.remove_processor';
42✔
114
        }
115

116
        return 'api_platform.doctrine.orm.state.persist_processor';
48✔
117
    }
118
}
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