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

api-platform / core / 13586524635

28 Feb 2025 10:45AM UTC coverage: 7.289%. Remained the same
13586524635

push

github

soyuka
docs: v4.0.19

12436 of 170622 relevant lines covered (7.29%)

11.99 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
    }
465✔
33

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

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

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

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

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

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

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

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

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

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

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

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

85
        return $resourceMetadataCollection;
84✔
86
    }
87

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

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

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

104
        return $operation;
36✔
105
    }
106

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

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

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

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