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

api-platform / core / 10814494863

11 Sep 2024 03:11PM UTC coverage: 7.008% (-0.7%) from 7.679%
10814494863

push

github

web-flow
fix(state): remove resource_class change (#6607)

11516 of 164321 relevant lines covered (7.01%)

22.85 hits per line

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

100.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\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\ODM\MongoDB\DocumentManager;
26
use Doctrine\Persistence\ManagerRegistry;
27

28
final class DoctrineMongoDbOdmResourceCollectionMetadataFactory implements ResourceMetadataCollectionFactoryInterface
29
{
30
    public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly ResourceMetadataCollectionFactoryInterface $decorated)
31
    {
32
    }
1,410✔
33

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

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

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

53
                    if (!$this->managerRegistry->getManagerForClass($documentClass) instanceof DocumentManager) {
4✔
54
                        continue;
4✔
55
                    }
56

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

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

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

65
            if ($graphQlOperations) {
4✔
66
                foreach ($graphQlOperations as $operationName => $graphQlOperation) {
4✔
67
                    if (!$this->managerRegistry->getManagerForClass($graphQlOperation->getClass()) instanceof DocumentManager) {
4✔
68
                        continue;
4✔
69
                    }
70

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

74
                $resourceMetadata = $resourceMetadata->withGraphQlOperations($graphQlOperations);
4✔
75
            }
76

77
            $resourceMetadataCollection[$i] = $resourceMetadata;
4✔
78
        }
79

80
        return $resourceMetadataCollection;
10✔
81
    }
82

83
    private function addDefaults(Operation $operation): Operation
84
    {
85
        $options = $operation->getStateOptions() ?: new Options();
2✔
86
        if ($options instanceof Options && null === $options->getHandleLinks()) {
2✔
87
            $options = $options->withHandleLinks('api_platform.doctrine.odm.links_handler');
2✔
88
            $operation = $operation->withStateOptions($options);
2✔
89
        }
90

91
        if (null === $operation->getProvider()) {
2✔
92
            $operation = $operation->withProvider($this->getProvider($operation));
2✔
93
        }
94

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

99
        return $operation;
2✔
100
    }
101

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

108
        return ItemProvider::class;
2✔
109
    }
110

111
    private function getProcessor(Operation $operation): string
112
    {
113
        if ($operation instanceof DeleteOperationInterface) {
2✔
114
            return 'api_platform.doctrine_mongodb.odm.state.remove_processor';
2✔
115
        }
116

117
        return 'api_platform.doctrine_mongodb.odm.state.persist_processor';
2✔
118
    }
119
}
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

© 2025 Coveralls, Inc