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

api-platform / core / 10537652610

24 Aug 2024 10:04AM UTC coverage: 7.707%. Remained the same
10537652610

push

github

dunglas
cleanup

12490 of 162060 relevant lines covered (7.71%)

22.98 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();
2,248✔
38
        $this->defaults = $defaults;
2,248✔
39
        $this->camelCaseToSnakeCaseNameConverter = new CamelCaseToSnakeCaseNameConverter();
2,248✔
40
    }
41

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

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

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

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

66
        return $resourceMetadataCollection;
4✔
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;
4✔
77
        $resources = [];
4✔
78
        foreach ($nodes as $node) {
4✔
79
            $resource = (new ApiResource())
4✔
80
                ->withShortName($shortName)
4✔
81
                ->withClass($resourceClass);
4✔
82
            foreach ($node as $key => $value) {
4✔
83
                $methodName = 'with'.ucfirst($key);
4✔
84
                if ('operations' !== $key && null !== $value && method_exists($resource, $methodName)) {
4✔
85
                    $resource = $resource->{$methodName}($value);
4✔
86
                }
87
            }
88

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

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

96
        return $resources;
4✔
97
    }
98

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

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

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

112
        foreach ($data as $attributes) {
4✔
113
            if (!class_exists($attributes['class'])) {
4✔
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());
4✔
119
            unset($attributes['class']);
4✔
120
            foreach ($attributes as $key => $value) {
4✔
121
                if (null === $value) {
4✔
122
                    continue;
4✔
123
                }
124

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

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

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

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

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

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

147
        if (null === $data) {
4✔
148
            return $this->addDefaultGraphQlOperations($resource);
4✔
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