Test Failed
Push — master ( 09495e...5db298 )
by Daniel
04:30
created

ComponentEventListener::onPostRead()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 9.9666
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 Silverback\ApiComponentsBundle\Entity\Core\ComponentInterface;
17
use Silverback\ApiComponentsBundle\Metadata\Factory\ComponentUsageMetadataFactory;
18
use Symfony\Component\HttpKernel\Event\RequestEvent;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
class ComponentEventListener
24
{
25
    private ComponentUsageMetadataFactory $metadataFactory;
26
27
    public function __construct(ComponentUsageMetadataFactory $metadataFactory)
28
    {
29
        $this->metadataFactory = $metadataFactory;
30
    }
31
32
    public function onPostRead(RequestEvent $event): void
33
    {
34
        $request = $event->getRequest();
35
        $data = $request->attributes->get('data');
36
        $operationName = $request->attributes->get('_api_item_operation_name');
37
        if (
38
            empty($data) ||
39
            !$data instanceof ComponentInterface ||
40
            'get_usage' !== $operationName
41
        ) {
42
            return;
43
        }
44
45
        $request->attributes->set('data', $this->metadataFactory->create($data));
46
    }
47
}
48