DefinitionLoaderListener   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 41
loc 41
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A getSubscribedEvents() 6 6 1
A loadDefinition() 14 14 2

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\Server\Definition\Locator;
7
use Symfony\Component\{
8
    EventDispatcher\EventSubscriberInterface,
9
    HttpKernel\KernelEvents,
10
    HttpKernel\Event\GetResponseEvent
11
};
12
13 View Code Duplication
final class DefinitionLoaderListener implements EventSubscriberInterface
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    private $locate;
16
17 8
    public function __construct(Locator $locator)
18
    {
19 8
        $this->locate = $locator;
20 8
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 38
    public static function getSubscribedEvents()
26
    {
27
        return [
28 38
            KernelEvents::REQUEST => [['loadDefinition', 30]],
29
        ];
30
    }
31
32
    /**
33
     * Inject in the request attributes the resource definition
34
     *
35
     * @param GetResponseEvent $event
36
     *
37
     * @return void
38
     */
39 6
    public function loadDefinition(GetResponseEvent $event)
40
    {
41 6
        $request = $event->getRequest();
42
43 6
        if (!$request->attributes->has('_innmind_resource')) {
44 2
            return;
45
        }
46
47 4
        $name = $request->attributes->get('_innmind_resource');
48 4
        $request->attributes->set(
49 4
            '_innmind_resource_definition',
50 4
            ($this->locate)($name)
51
        );
52 2
    }
53
}
54