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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

69.44
/src/Metadata/Resource/Factory/ExtractorResourceMetadataCollectionFactory.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\Extractor\ResourceExtractorInterface;
18
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\Operations;
21
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
22
use ApiPlatform\Metadata\Util\CamelCaseToSnakeCaseNameConverter;
23
use Psr\Log\LoggerInterface;
24
use Psr\Log\NullLogger;
25

26
/**
27
 * Creates a resource metadata from {@see Resource} extractors (XML, YAML).
28
 *
29
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
30
 */
31
final class ExtractorResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
32
{
33
    use OperationDefaultsTrait;
34

35
    public function __construct(private readonly ResourceExtractorInterface $extractor, private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, array $defaults = [], ?LoggerInterface $logger = null, private readonly bool $graphQlEnabled = false)
36
    {
37
        $this->logger = $logger ?? new NullLogger();
1,214✔
38
        $this->defaults = $defaults;
1,214✔
39
        $this->camelCaseToSnakeCaseNameConverter = new CamelCaseToSnakeCaseNameConverter();
1,214✔
40
    }
41

42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function create(string $resourceClass): ResourceMetadataCollection
46
    {
47
        $resourceMetadataCollection = new ResourceMetadataCollection($resourceClass);
101✔
48
        if ($this->decorated) {
101✔
49
            $resourceMetadataCollection = $this->decorated->create($resourceClass);
101✔
50
        }
51

52
        if (!(class_exists($resourceClass) || interface_exists($resourceClass)) || !$resources = $this->extractor->getResources()[$resourceClass] ?? false) {
101✔
53
            return $resourceMetadataCollection;
101✔
54
        }
55

56
        foreach ($this->buildResources($resources, $resourceClass) as $resource) {
3✔
57
            foreach ($this->defaults['attributes'] ?? [] as $key => $value) {
3✔
58
                if (method_exists($resource, 'get'.ucfirst($key)) && !$resource->{'get'.ucfirst($key)}()) {
×
59
                    $resource = $resource->{'with'.ucfirst($key)}($value);
×
60
                }
61
            }
62

63
            $resourceMetadataCollection[] = $resource;
3✔
64
        }
65

66
        return $resourceMetadataCollection;
3✔
67
    }
68

69
    /**
70
     * Builds resources to support:.
71
     *
72
     * @return ApiResource[]
73
     */
74
    private function buildResources(array $nodes, string $resourceClass): array
75
    {
76
        $shortName = (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass;
3✔
77
        $resources = [];
3✔
78
        foreach ($nodes as $node) {
3✔
79
            $resource = (new ApiResource())
3✔
80
                ->withShortName($shortName)
3✔
81
                ->withClass($resourceClass);
3✔
82
            foreach ($node as $key => $value) {
3✔
83
                $methodName = 'with'.ucfirst($key);
3✔
84
                if ('operations' !== $key && null !== $value && method_exists($resource, $methodName)) {
3✔
UNCOV
85
                    $resource = $resource->{$methodName}($value);
1✔
86
                }
87
            }
88

89
            if ($this->graphQlEnabled) {
3✔
90
                $resource = $this->addGraphQlOperations($node['graphQlOperations'] ?? null, $resource);
3✔
91
            }
92

93
            $resources[] = $this->addOperations($node['operations'] ?? null, $resource);
3✔
94
        }
95

96
        return $resources;
3✔
97
    }
98

99
    private function addOperations(?array $data, ApiResource $resource): ApiResource
100
    {
101
        $operations = [];
3✔
102

103
        if (null === $data) {
3✔
UNCOV
104
            foreach ($this->getDefaultHttpOperations($resource) as $operation) {
1✔
UNCOV
105
                [$key, $operation] = $this->getOperationWithDefaults($resource, $operation);
1✔
UNCOV
106
                $operations[$key] = $operation;
1✔
107
            }
108

UNCOV
109
            return $resource->withOperations(new Operations($operations));
1✔
110
        }
111

112
        foreach ($data as $attributes) {
3✔
113
            if (!class_exists($attributes['class'])) {
3✔
114
                throw new \InvalidArgumentException(\sprintf('Operation "%s" does not exist.', $attributes['class']));
×
115
            }
116

117
            /** @var HttpOperation $operation */
118
            $operation = (new $attributes['class']())->withShortName($resource->getShortName());
3✔
119
            unset($attributes['class']);
3✔
120
            foreach ($attributes as $key => $value) {
3✔
121
                if (null === $value) {
3✔
122
                    continue;
3✔
123
                }
124

UNCOV
125
                $camelCaseKey = $this->camelCaseToSnakeCaseNameConverter->denormalize($key);
1✔
UNCOV
126
                $methodName = 'with'.ucfirst($camelCaseKey);
1✔
127

UNCOV
128
                if (method_exists($operation, $methodName)) {
1✔
UNCOV
129
                    $operation = $operation->{$methodName}($value);
1✔
UNCOV
130
                    continue;
1✔
131
                }
132

133
                $operation = $operation->withExtraProperties(array_merge($operation->getExtraProperties(), [$key => $value]));
×
134
            }
135

136
            [$key, $operation] = $this->getOperationWithDefaults($resource, $operation);
3✔
137
            $operations[$key] = $operation;
3✔
138
        }
139

140
        return $resource->withOperations(new Operations($operations));
3✔
141
    }
142

143
    private function addGraphQlOperations(?array $data, ApiResource $resource): ApiResource
144
    {
145
        $operations = [];
3✔
146

147
        if (null === $data) {
3✔
148
            return $this->addDefaultGraphQlOperations($resource);
3✔
149
        }
150

151
        foreach ($data as $attributes) {
×
152
            if (!class_exists($attributes['class'])) {
×
153
                throw new \InvalidArgumentException(\sprintf('Operation "%s" does not exist.', $attributes['class']));
×
154
            }
155

156
            /** @var GraphQlOperation $operation */
157
            $operation = (new $attributes['class']())->withShortName($resource->getShortName());
×
158
            unset($attributes['class']);
×
159
            foreach ($attributes as $key => $value) {
×
160
                if (null === $value) {
×
161
                    continue;
×
162
                }
163

164
                $camelCaseKey = $this->camelCaseToSnakeCaseNameConverter->denormalize($key);
×
165
                $methodName = 'with'.ucfirst($camelCaseKey);
×
166

167
                if (method_exists($operation, $methodName)) {
×
168
                    $operation = $operation->{$methodName}($value);
×
169
                    continue;
×
170
                }
171

172
                $operation = $operation->withExtraProperties(array_merge($operation->getExtraProperties(), [$key => $value]));
×
173
            }
174

175
            [$key, $operation] = $this->getOperationWithDefaults($resource, $operation);
×
176
            $operations[$key] = $operation;
×
177
        }
178

179
        $resource = $resource->withGraphQlOperations($operations);
×
180

181
        return $this->completeGraphQlOperations($resource);
×
182
    }
183
}
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