Completed
Push — master ( d4cfd0...1c3910 )
by Antoine
18s queued 10s
created

AttributesExtractor::extractAttributes()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 20
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
39
        if ($subresourceContext = $attributes['_api_subresource_context'] ?? null) {
40
            $result['subresource_context'] = $subresourceContext;
41
        }
42
43
        if (null === $result['resource_class']) {
44
            return [];
45
        }
46
47
        $hasRequestAttributeKey = false;
48
        foreach (OperationType::TYPES as $operationType) {
49
            $attribute = "_api_{$operationType}_operation_name";
50
            if (isset($attributes[$attribute])) {
51
                $result["{$operationType}_operation_name"] = $attributes[$attribute];
52
                $hasRequestAttributeKey = true;
53
                break;
54
            }
55
        }
56
57
        if (false === $hasRequestAttributeKey) {
58
            return [];
59
        }
60
61
        if (null === $apiRequest = $attributes['_api_receive'] ?? null) {
62
            $result['receive'] = true;
63
        } else {
64
            $result['receive'] = (bool) $apiRequest;
65
        }
66
67
        return $result;
68
    }
69
}
70