Completed
Push — master ( b15030...4f12b2 )
by GRASSIOT
22s queued 10s
created

AttributesExtractor::extractAttributes()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 34
rs 8.9777
c 0
b 0
f 0
cc 6
nc 14
nop 1
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\Util;
15
16
use ApiPlatform\Core\Api\OperationType;
17
18
/**
19
 * Extracts data used by the library form given attributes.
20
 *
21
 * @author Antoine Bluchet <[email protected]>
22
 *
23
 * @internal
24
 */
25
final class AttributesExtractor
26
{
27
    private function __construct()
28
    {
29
    }
30
31
    /**
32
     * Extracts resource class, operation name and format request attributes. Returns an empty array if the request does
33
     * not contain required attributes.
34
     */
35
    public static function extractAttributes(array $attributes): array
36
    {
37
        $result = ['resource_class' => $attributes['_api_resource_class'] ?? null];
38
        $result['input_class'] = $attributes['_api_input_class'] ?? $result['resource_class'];
39
        $result['output_class'] = $attributes['_api_output_class'] ?? $result['resource_class'];
40
41
        if ($subresourceContext = $attributes['_api_subresource_context'] ?? null) {
42
            $result['subresource_context'] = $subresourceContext;
43
        }
44
45
        if (null === $result['resource_class']) {
46
            return [];
47
        }
48
49
        $hasRequestAttributeKey = false;
50
        foreach (OperationType::TYPES as $operationType) {
51
            $attribute = "_api_{$operationType}_operation_name";
52
            if (isset($attributes[$attribute])) {
53
                $result["{$operationType}_operation_name"] = $attributes[$attribute];
54
                $hasRequestAttributeKey = true;
55
                break;
56
            }
57
        }
58
59
        if (false === $hasRequestAttributeKey) {
60
            return [];
61
        }
62
63
        $result += [
64
            'receive' => (bool) ($attributes['_api_receive'] ?? true),
65
            'persist' => (bool) ($attributes['_api_persist'] ?? true),
66
        ];
67
68
        return $result;
69
    }
70
}
71