Passed
Push — master ( 2bb6f9...3a8224 )
by Kévin
06:14 queued 02:58
created

AttributesExtractor::extractAttributes()   A

Complexity

Conditions 6
Paths 14

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 14
nop 1
dl 0
loc 31
rs 9.0444
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\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
        if ($subresourceContext = $attributes['_api_subresource_context'] ?? null) {
39
            $result['subresource_context'] = $subresourceContext;
40
        }
41
42
        if (null === $result['resource_class']) {
43
            return [];
44
        }
45
46
        $hasRequestAttributeKey = false;
47
        foreach (OperationType::TYPES as $operationType) {
48
            $attribute = "_api_{$operationType}_operation_name";
49
            if (isset($attributes[$attribute])) {
50
                $result["{$operationType}_operation_name"] = $attributes[$attribute];
51
                $hasRequestAttributeKey = true;
52
                break;
53
            }
54
        }
55
56
        if (false === $hasRequestAttributeKey) {
57
            return [];
58
        }
59
60
        $result += [
61
            'receive' => (bool) ($attributes['_api_receive'] ?? true),
62
            'persist' => (bool) ($attributes['_api_persist'] ?? true),
63
        ];
64
65
        return $result;
66
    }
67
}
68