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

ApiEventListenerTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 21
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A getAttributes() 0 19 4
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