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

api-platform / core / 10729306835

05 Sep 2024 10:46PM UTC coverage: 7.655% (-0.01%) from 7.665%
10729306835

push

github

web-flow
Merge pull request #6586 from soyuka/merge-342

Merge 3.4

0 of 54 new or added lines in 12 files covered. (0.0%)

8760 existing lines in 277 files now uncovered.

12505 of 163357 relevant lines covered (7.66%)

22.84 hits per line

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

95.4
/src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.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\Metadata\Resource\Factory;
15

16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\Metadata;
21
use ApiPlatform\Metadata\Operations;
22
use ApiPlatform\Metadata\Parameter;
23
use ApiPlatform\Metadata\Parameters;
24
use ApiPlatform\Metadata\Util\CamelCaseToSnakeCaseNameConverter;
25
use Psr\Log\LoggerInterface;
26
use Psr\Log\NullLogger;
27

28
/**
29
 * @internal
30
 *
31
 * This trait shares the common logic between attributes and Laravel concerns factories
32
 *
33
 * @author Antoine Bluchet <soyuka@gmail.com>
34
 * @author Kévin Dunglas <kevin@dunglas.dev>
35
 */
36
trait MetadataCollectionFactoryTrait
37
{
38
    use OperationDefaultsTrait;
39

40
    public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, ?LoggerInterface $logger = null, array $defaults = [], private readonly bool $graphQlEnabled = false)
41
    {
UNCOV
42
        $this->logger = $logger ?? new NullLogger();
2,248✔
UNCOV
43
        $this->defaults = $defaults;
2,248✔
UNCOV
44
        $this->camelCaseToSnakeCaseNameConverter = new CamelCaseToSnakeCaseNameConverter();
2,248✔
45
    }
46

47
    private function isResourceMetadata(string $name): bool
48
    {
UNCOV
49
        return is_a($name, ApiResource::class, true) || is_subclass_of($name, HttpOperation::class) || is_subclass_of($name, GraphQlOperation::class) || is_a($name, Parameter::class, true);
42✔
50
    }
51

52
    /**
53
     * Builds resource operations to support:
54
     * Resource
55
     * Get
56
     * Post
57
     * Resource
58
     * Get
59
     * In the future, we will be able to use nested attributes (https://wiki.php.net/rfc/new_in_initializers).
60
     *
61
     * @param array<Metadata|Parameter> $metadataCollection
62
     *
63
     * @return ApiResource[]
64
     */
65
    private function buildResourceOperations(array $metadataCollection, string $resourceClass): array
66
    {
UNCOV
67
        $shortName = (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass;
42✔
UNCOV
68
        $resources = [];
42✔
UNCOV
69
        $index = -1;
42✔
UNCOV
70
        $operationPriority = 0;
42✔
UNCOV
71
        $hasApiResource = false;
42✔
UNCOV
72
        $globalParameters = new Parameters();
42✔
73

UNCOV
74
        foreach ($metadataCollection as $metadata) {
42✔
UNCOV
75
            if ($metadata instanceof Parameter) {
34✔
UNCOV
76
                if (!$k = $metadata->getKey()) {
3✔
77
                    throw new RuntimeException('Parameter "key" is mandatory when used on a class.');
×
78
                }
UNCOV
79
                $globalParameters->add($k, $metadata);
3✔
UNCOV
80
                continue;
3✔
81
            }
82

UNCOV
83
            if ($metadata instanceof ApiResource) {
34✔
UNCOV
84
                $hasApiResource = true;
31✔
UNCOV
85
                $resource = $this->getResourceWithDefaults($resourceClass, $shortName, $metadata);
31✔
UNCOV
86
                $operations = [];
31✔
UNCOV
87
                foreach ($resource->getOperations() ?? new Operations() as $operation) {
31✔
UNCOV
88
                    [$key, $operation] = $this->getOperationWithDefaults($resource, $operation);
20✔
UNCOV
89
                    $operations[$key] = $operation;
20✔
90
                }
UNCOV
91
                if ($operations) {
31✔
UNCOV
92
                    $resource = $resource->withOperations(new Operations($operations));
20✔
93
                }
UNCOV
94
                $resources[++$index] = $resource;
31✔
UNCOV
95
                continue;
31✔
96
            }
97

UNCOV
98
            if (!is_subclass_of($metadata, HttpOperation::class) && !is_subclass_of($metadata, GraphQlOperation::class)) {
7✔
99
                continue;
×
100
            }
101

UNCOV
102
            if ($metadata instanceof GraphQlOperation) {
7✔
UNCOV
103
                [$key, $operation] = $this->getOperationWithDefaults($resources[$index], $metadata);
3✔
UNCOV
104
                $graphQlOperations = $resources[$index]->getGraphQlOperations();
3✔
UNCOV
105
                $graphQlOperations[$key] = $operation;
3✔
UNCOV
106
                $resources[$index] = $resources[$index]->withGraphQlOperations($graphQlOperations);
3✔
UNCOV
107
                continue;
3✔
108
            }
109

UNCOV
110
            if (-1 === $index || $this->hasSameOperation($resources[$index], $metadata::class, $metadata)) {
7✔
UNCOV
111
                $resources[++$index] = $this->getResourceWithDefaults($resourceClass, $shortName, new ApiResource());
7✔
112
            }
113

UNCOV
114
            [$key, $operation] = $this->getOperationWithDefaults($resources[$index], $metadata);
7✔
UNCOV
115
            if (null === $operation->getPriority()) {
7✔
UNCOV
116
                $operation = $operation->withPriority(++$operationPriority);
7✔
117
            }
UNCOV
118
            $operations = $resources[$index]->getOperations() ?? new Operations();
7✔
UNCOV
119
            $resources[$index] = $resources[$index]->withOperations($operations->add($key, $operation));
7✔
120
        }
121

122
        // Loop again and set default operations if none where found
UNCOV
123
        foreach ($resources as $index => $resource) {
42✔
UNCOV
124
            if (\count($globalParameters) > 0) {
34✔
UNCOV
125
                $resources[$index] = $resource = $this->mergeOperationParameters($resource, $globalParameters);
3✔
126
            }
127

UNCOV
128
            if (null === $resource->getOperations()) {
34✔
UNCOV
129
                $operations = [];
23✔
UNCOV
130
                foreach ($this->getDefaultHttpOperations($resource) as $operation) {
23✔
UNCOV
131
                    [$key, $operation] = $this->getOperationWithDefaults($resource, $operation, true);
23✔
UNCOV
132
                    $operations[$key] = $operation;
23✔
133
                }
UNCOV
134
                $resources[$index] = $resource = $resource->withOperations(new Operations($operations));
23✔
135
            }
136

UNCOV
137
            if ($parameters = $resource->getParameters()) {
34✔
UNCOV
138
                $operations = [];
3✔
UNCOV
139
                foreach ($resource->getOperations() ?? [] as $i => $operation) {
3✔
UNCOV
140
                    $operations[$i] = $this->mergeOperationParameters($operation, $parameters);
3✔
141
                }
UNCOV
142
                $resources[$index] = $resource = $resource->withOperations(new Operations($operations)); // @phpstan-ignore-line
3✔
143
            }
144

UNCOV
145
            $graphQlOperations = $resource->getGraphQlOperations();
34✔
UNCOV
146
            if (!$this->graphQlEnabled) {
34✔
147
                continue;
×
148
            }
149

UNCOV
150
            if (null === $graphQlOperations) {
34✔
UNCOV
151
                if (!$hasApiResource) {
31✔
UNCOV
152
                    $resources[$index] = $resources[$index]->withGraphQlOperations([]);
7✔
UNCOV
153
                    continue;
7✔
154
                }
155

156
                // Add default GraphQL operations on the first resource
UNCOV
157
                if (0 === $index) {
28✔
UNCOV
158
                    $resources[$index] = $this->addDefaultGraphQlOperations($resources[$index]);
24✔
159
                }
UNCOV
160
                continue;
28✔
161
            }
162

UNCOV
163
            $resources[$index] = $this->completeGraphQlOperations($resources[$index]);
11✔
UNCOV
164
            $graphQlOperations = $resources[$index]->getGraphQlOperations();
11✔
165

UNCOV
166
            $graphQlOperationsWithDefaults = [];
11✔
UNCOV
167
            foreach ($graphQlOperations as $operation) {
11✔
UNCOV
168
                [$key, $operation] = $this->getOperationWithDefaults($resource, $operation);
11✔
UNCOV
169
                if ($parameters) {
11✔
UNCOV
170
                    $operation = $this->mergeOperationParameters($operation, $parameters);
3✔
171
                }
172

UNCOV
173
                $graphQlOperationsWithDefaults[$key] = $operation;
11✔
174
            }
175

UNCOV
176
            $resources[$index] = $resources[$index]->withGraphQlOperations($graphQlOperationsWithDefaults);
11✔
177
        }
178

UNCOV
179
        return $resources;
42✔
180
    }
181

182
    /**
183
     * Does the resource already have an operation of the $operationClass type?
184
     * Useful to determine if we need to create a new ApiResource when the class has only operation attributes, for example:.
185
     *
186
     * #[Get]
187
     * #[Get(uriTemplate: '/alternate')]
188
     * class Example {}
189
     */
190
    private function hasSameOperation(ApiResource $resource, string $operationClass, HttpOperation $operation): bool
191
    {
UNCOV
192
        foreach ($resource->getOperations() ?? [] as $o) {
4✔
UNCOV
193
            if ($o instanceof $operationClass && $operation->getUriTemplate() === $o->getUriTemplate() && $operation->getName() === $o->getName() && $operation->getRouteName() === $o->getRouteName()) {
4✔
194
                return true;
×
195
            }
196
        }
197

UNCOV
198
        return false;
4✔
199
    }
200

201
    /**
202
     * @template T of Metadata
203
     *
204
     * @param T $resource
205
     *
206
     * @return T
207
     */
208
    private function mergeOperationParameters(Metadata $resource, Parameters $globalParameters): Metadata
209
    {
UNCOV
210
        $parameters = $resource->getParameters() ?? new Parameters();
3✔
UNCOV
211
        foreach ($globalParameters as $parameterName => $parameter) {
3✔
UNCOV
212
            if ($key = $parameter->getKey()) {
3✔
UNCOV
213
                $parameterName = $key;
3✔
214
            }
215

UNCOV
216
            if (!$parameters->has($parameterName, $parameter::class)) {
3✔
UNCOV
217
                $parameters->add($parameterName, $parameter);
3✔
218
            }
219
        }
220

UNCOV
221
        return $resource->withParameters($parameters);
3✔
222
    }
223
}
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