Test Failed
Pull Request — main (#140)
by Daniel
04:22
created

ApiEventListenerTrait::getAttributes()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
nc 3
nop 1
dl 0
loc 19
rs 9.7998
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[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 Silverback\ApiComponentsBundle\EventListener\Api;
15
16
use ApiPlatform\Metadata\Operation;
17
use JetBrains\PhpStorm\ArrayShape;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
trait ApiEventListenerTrait
24
{
25
    #[ArrayShape(['data' => 'mixed', 'class' => 'string', 'operation' => Operation::class])]
26
    private function getAttributes(Request $request): array
27
    {
28
        $operation = $request->attributes->get('_api_operation');
29
        $data = $request->attributes->get('data');
30
        $class = null;
31
        if (\is_object($data)) {
32
            $class = \get_class($data);
33
        } else {
34
            $normalizationContext = $request->attributes->get('_api_normalization_context');
35
            if ($normalizationContext && isset($normalizationContext['resource_class'])) {
36
                $class = $normalizationContext['resource_class'];
37
            }
38
        }
39
40
        return [
41
            'data' => $data,
42
            'class' => $class,
43
            'operation' => $operation,
44
        ];
45
    }
46
}
47