Completed
Push — master ( d2b1b9...c80d49 )
by Antoine
22s queued 13s
created

getTransformedOperations()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 6
nop 2
dl 0
loc 12
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Factory;
15
16
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
17
18
/**
19
 * Transforms the given input/output metadata to a normalized one.
20
 *
21
 * @author Antoine Bluchet <[email protected]>
22
 */
23
final class InputOutputResourceMetadataFactory implements ResourceMetadataFactoryInterface
24
{
25
    private $decorated;
26
27
    public function __construct(ResourceMetadataFactoryInterface $decorated)
28
    {
29
        $this->decorated = $decorated;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function create(string $resourceClass): ResourceMetadata
36
    {
37
        $resourceMetadata = $this->decorated->create($resourceClass);
38
39
        $attributes = $resourceMetadata->getAttributes() ?: [];
40
        $attributes['input'] = isset($attributes['input']) ? $this->transformInputOutput($attributes['input']) : null;
41
        $attributes['output'] = isset($attributes['output']) ? $this->transformInputOutput($attributes['output']) : null;
42
43
        if (null !== $collectionOperations = $resourceMetadata->getCollectionOperations()) {
44
            $resourceMetadata = $resourceMetadata->withCollectionOperations($this->getTransformedOperations($collectionOperations, $attributes));
45
        }
46
47
        if (null !== $itemOperations = $resourceMetadata->getItemOperations()) {
48
            $resourceMetadata = $resourceMetadata->withItemOperations($this->getTransformedOperations($itemOperations, $attributes));
49
        }
50
51
        return $resourceMetadata->withAttributes($attributes);
52
    }
53
54
    private function getTransformedOperations(array $operations, array $resourceAttributes): array
55
    {
56
        foreach ($operations as $key => &$operation) {
57
            if (!\is_array($operation)) {
58
                continue;
59
            }
60
61
            $operation['input'] = isset($operation['input']) ? $this->transformInputOutput($operation['input']) : $resourceAttributes['input'];
62
            $operation['output'] = isset($operation['output']) ? $this->transformInputOutput($operation['output']) : $resourceAttributes['output'];
63
        }
64
65
        return $operations;
66
    }
67
68
    private function transformInputOutput($attribute): ?array
69
    {
70
        if (null === $attribute) {
71
            return null;
72
        }
73
74
        if (false === $attribute) {
75
            return ['class' => null];
76
        }
77
78
        if (\is_string($attribute)) {
79
            $attribute = ['class' => $attribute];
80
        }
81
82
        if (!isset($attribute['name'])) {
83
            $attribute['name'] = (new \ReflectionClass($attribute['class']))->getShortName();
84
        }
85
86
        return $attribute;
87
    }
88
}
89