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

api-platform / core / 6693023961

30 Oct 2023 12:51PM UTC coverage: 67.319%. Remained the same
6693023961

push

github

web-flow
ci: php cs fixer (#5905)

--


fix cs


fix last cs

259 of 259 new or added lines in 83 files covered. (100.0%)

15610 of 23188 relevant lines covered (67.32%)

10.87 hits per line

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

0.0
/src/Core/Metadata/Resource/ApiResourceToLegacyResourceMetadataTrait.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\Core\Metadata\Resource;
15

16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\CollectionOperationInterface;
18
use ApiPlatform\Metadata\HttpOperation;
19
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
20

21
/**
22
 * @internal
23
 *
24
 * @deprecated
25
 */
26
trait ApiResourceToLegacyResourceMetadataTrait
27
{
28
    private $camelCaseToSnakeCaseNameConverter;
29

30
    private function transformResourceToResourceMetadata(ApiResource $resource): ResourceMetadata
31
    {
32
        $collectionOperations = [];
×
33
        $itemOperations = [];
×
34
        foreach ($resource->getOperations() as $name => $operation) {
×
35
            $arrayOperation = $this->toArray($operation);
×
36

37
            if (!isset($arrayOperation['openapi_context'])) {
×
38
                $arrayOperation['openapi_context'] = [];
×
39
            }
40

41
            $arrayOperation['openapi_context']['operationId'] = $name;
×
42
            $arrayOperation['composite_identifier'] = $this->hasCompositeIdentifier($operation);
×
43

44
            if (HttpOperation::METHOD_POST === $operation->getMethod() && !$operation->getUriVariables()) {
×
45
                $collectionOperations[$name] = $arrayOperation;
×
46
                continue;
×
47
            }
48

49
            if ($operation instanceof CollectionOperationInterface) {
×
50
                $collectionOperations[$name] = $arrayOperation;
×
51
                continue;
×
52
            }
53

54
            $itemOperations[$name] = $arrayOperation;
×
55
        }
56

57
        $attributes = $this->toArray($resource);
×
58

59
        $graphqlOperations = $resource->getGraphQlOperations() ? [] : null;
×
60
        foreach ($resource->getGraphQlOperations() ?? [] as $operationName => $operation) {
×
61
            $graphqlOperations[$operationName] = $this->toArray($operation);
×
62
        }
63

64
        return new ResourceMetadata($resource->getShortName(), $resource->getDescription(), $resource->getTypes()[0] ?? null, $itemOperations, $collectionOperations, $attributes, null, $graphqlOperations);
×
65
    }
66

67
    private function toArray($object): array
68
    {
69
        if (!$this->camelCaseToSnakeCaseNameConverter) {
×
70
            $this->camelCaseToSnakeCaseNameConverter = new CamelCaseToSnakeCaseNameConverter();
×
71
        }
72

73
        $arr = [];
×
74
        foreach (get_class_methods($object) as $methodName) {
×
75
            if ('getOperations' === $methodName || !str_starts_with($methodName, 'get')) {
×
76
                continue;
×
77
            }
78

79
            if (null === $value = $object->{$methodName}()) {
×
80
                continue;
×
81
            }
82

83
            $arr[$this->camelCaseToSnakeCaseNameConverter->normalize(lcfirst(substr($methodName, 3)))] = $value;
×
84
        }
85

86
        return $this->transformUriVariablesToIdentifiers($arr);
×
87
    }
88

89
    private function transformUriVariablesToIdentifiers(array $arrayOperation): array
90
    {
91
        if (!isset($arrayOperation['uri_variables'])) {
×
92
            return $arrayOperation;
×
93
        }
94

95
        if (!\is_array($arrayOperation['uri_variables'])) {
×
96
            $arrayOperation['identifiers'] = $arrayOperation['uri_variables'];
×
97

98
            return $arrayOperation;
×
99
        }
100

101
        $arrayOperation['identifiers'] = [];
×
102
        foreach ($arrayOperation['uri_variables'] as $parameterName => $identifiedBy) {
×
103
            if ($identifiedBy->getExpandedValue() ?? false) {
×
104
                continue;
×
105
            }
106

107
            if (1 === \count($identifiedBy->getIdentifiers() ?? ['id'])) {
×
108
                $arrayOperation['identifiers'][$parameterName] = [$identifiedBy->getFromClass(), $identifiedBy->getIdentifiers()[0] ?? ['id']];
×
109
                continue;
×
110
            }
111

112
            foreach ($identifiedBy->getIdentifiers() as $identifier) {
×
113
                $arrayOperation['identifiers'][$identifier] = [$identifiedBy->getFromClass(), $identifier];
×
114
            }
115
        }
116

117
        return $arrayOperation;
×
118
    }
119

120
    private function hasCompositeIdentifier(HttpOperation $operation): bool
121
    {
122
        foreach ($operation->getUriVariables() ?? [] as $parameterName => $uriVariable) {
×
123
            if ($uriVariable->getCompositeIdentifier()) {
×
124
                return true;
×
125
            }
126
        }
127

128
        return false;
×
129
    }
130
}
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