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

api-platform / core / 14400511848

11 Apr 2025 09:53AM UTC coverage: 8.488%. Remained the same
14400511848

push

github

soyuka
docs: changelog 4.1.7

13402 of 157890 relevant lines covered (8.49%)

22.86 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,351✔
33

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

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

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

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

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

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

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

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

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

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

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

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

85
        return $resourceMetadataCollection;
104✔
86
    }
87

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

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

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

104
        return $operation;
62✔
105
    }
106

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

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

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

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