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

api-platform / core / 15927526370

27 Jun 2025 01:30PM UTC coverage: 22.016% (-0.008%) from 22.024%
15927526370

push

github

web-flow
refactor(metadata): cascade resource to operation (#7246)

12 of 46 new or added lines in 12 files covered. (26.09%)

63 existing lines in 2 files now uncovered.

11494 of 52207 relevant lines covered (22.02%)

21.67 hits per line

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

0.0
/src/Doctrine/Odm/Metadata/Resource/DoctrineMongoDbOdmResourceCollectionMetadataFactory.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\Odm\Metadata\Resource;
15

16
use ApiPlatform\Doctrine\Odm\State\CollectionProvider;
17
use ApiPlatform\Doctrine\Odm\State\ItemProvider;
18
use ApiPlatform\Doctrine\Odm\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\ODM\MongoDB\DocumentManager;
26
use Doctrine\Persistence\ManagerRegistry;
27

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

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

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

UNCOV
43
        foreach ($resourceMetadataCollection as $i => $resourceMetadata) {
×
44
            $operations = $resourceMetadata->getOperations();
×
45

46
            if ($operations) {
×
UNCOV
47
                foreach ($resourceMetadata->getOperations() as $operationName => $operation) {
×
48
                    $documentClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);
×
49
                    if (!$this->managerRegistry->getManagerForClass($documentClass) instanceof DocumentManager) {
×
50
                        continue;
×
51
                    }
52

53
                    $operations->add($operationName, $this->addDefaults($operation));
×
54
                }
55

56
                $resourceMetadata = $resourceMetadata->withOperations($operations);
×
57
            }
58

59
            $graphQlOperations = $resourceMetadata->getGraphQlOperations();
×
60

61
            if ($graphQlOperations) {
×
62
                foreach ($graphQlOperations as $operationName => $graphQlOperation) {
×
63
                    if (!$this->managerRegistry->getManagerForClass($graphQlOperation->getClass()) instanceof DocumentManager) {
×
64
                        continue;
×
65
                    }
66

67
                    $graphQlOperations[$operationName] = $this->addDefaults($graphQlOperation);
×
68
                }
69

70
                $resourceMetadata = $resourceMetadata->withGraphQlOperations($graphQlOperations);
×
71
            }
72

73
            $resourceMetadataCollection[$i] = $resourceMetadata;
×
74
        }
75

76
        return $resourceMetadataCollection;
×
77
    }
78

79
    private function addDefaults(Operation $operation): Operation
80
    {
81
        $options = $operation->getStateOptions() ?: new Options();
×
82
        if ($options instanceof Options && null === $options->getHandleLinks()) {
×
83
            $options = $options->withHandleLinks('api_platform.doctrine.odm.links_handler');
×
84
            $operation = $operation->withStateOptions($options);
×
85
        }
86

87
        if (null === $operation->getProvider()) {
×
88
            $operation = $operation->withProvider($this->getProvider($operation));
×
89
        }
90

91
        if (null === $operation->getProcessor()) {
×
92
            $operation = $operation->withProcessor($this->getProcessor($operation));
×
93
        }
94

95
        return $operation;
×
96
    }
97

98
    private function getProvider(Operation $operation): string
99
    {
100
        if ($operation instanceof CollectionOperationInterface) {
×
101
            return CollectionProvider::class;
×
102
        }
103

104
        return ItemProvider::class;
×
105
    }
106

107
    private function getProcessor(Operation $operation): string
108
    {
109
        if ($operation instanceof DeleteOperationInterface) {
×
110
            return 'api_platform.doctrine_mongodb.odm.state.remove_processor';
×
111
        }
112

113
        return 'api_platform.doctrine_mongodb.odm.state.persist_processor';
×
114
    }
115
}
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