Completed
Push — develop ( dd6a23...99c8cd )
by Baptiste
06:26
created

DefinitionLoaderListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\EventListener;
5
6
use Innmind\Rest\ServerBundle\Exception\{
7
    InvalidArgumentException,
8
    DefinitionNotFoundException
9
};
10
use Innmind\Rest\Server\Definition\{
11
    Directory,
12
    HttpResource
13
};
14
use Innmind\Immutable\MapInterface;
15
use Symfony\Component\{
16
    EventDispatcher\EventSubscriberInterface,
17
    HttpKernel\KernelEvents,
18
    HttpKernel\Event\GetResponseEvent
19
};
20
21
final class DefinitionLoaderListener implements EventSubscriberInterface
22
{
23
    private $directories;
24
25 5 View Code Duplication
    public function __construct(MapInterface $directories)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        if (
28 5
            (string) $directories->keyType() !== 'string' ||
29 5
            (string) $directories->valueType() !== Directory::class
30
        ) {
31 1
            throw new InvalidArgumentException;
32
        }
33
34 4
        $this->directories = $directories;
35 4
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 1
    public static function getSubscribedEvents()
41
    {
42
        return [
43 1
            KernelEvents::REQUEST => [['loadDefinition', 30]],
44
        ];
45
    }
46
47
    /**
48
     * Inject in the request attributes the resource definition
49
     *
50
     * @param GetResponseEvent $event
51
     *
52
     * @return void
53
     */
54 3
    public function loadDefinition(GetResponseEvent $event)
55
    {
56 3
        $request = $event->getRequest();
57
58 3
        if (!$request->attributes->has('_innmind_resource')) {
59 1
            return;
60
        }
61
62 2
        $name = $request->attributes->get('_innmind_resource');
63
        $resource = $this
64 2
            ->directories
65 2
            ->reduce(
66 2
                null,
67 2
                function($carry, string $dirName, Directory $directory) use ($name) {
68 2
                    if ($carry instanceof $directory) {
69
                        return $carry;
70
                    }
71
72 2
                    $resources = $directory->flatten();
73
74 2
                    if ($resources->contains($name)) {
75 1
                        return $resources->get($name);
76
                    }
77 2
                }
78
            );
79
80 2
        if (!$resource instanceof HttpResource) {
81 1
            throw new DefinitionNotFoundException;
82
        }
83
84 1
        $request->attributes->set(
85 1
            '_innmind_resource_definition',
86
            $resource
87
        );
88 1
    }
89
}
90