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

DefinitionLoaderListener   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 15.94 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 11
loc 69
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 3
A getSubscribedEvents() 0 6 1
B loadDefinition() 0 35 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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