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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 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
    }
588✔
35

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

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

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

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

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

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

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

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

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

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

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

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

79
        return $resourceMetadataCollection;
102✔
80
    }
81

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

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

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

98
        return $operation;
52✔
99
    }
100

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

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

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

116
        return 'api_platform.doctrine.orm.state.persist_processor';
52✔
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