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

RouteLoader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 95.92%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 12
dl 14
loc 105
ccs 47
cts 49
cp 0.9592
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 3
B load() 0 31 3
A supports() 0 4 1
A buildDirectoryRoutes() 0 15 1
B buildResourceRoutes() 0 24 3

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\Routing;
5
6
use Innmind\Rest\ServerBundle\Exception\{
7
    InvalidArgumentException,
8
    RouteLoaderLoadedMultipleTimesException
9
};
10
use Innmind\Rest\Server\{
11
    Definition\Directory,
12
    Definition\HttpResource,
13
    Action
14
};
15
use Innmind\Http\Message\MethodInterface;
16
use Innmind\Immutable\{
17
    MapInterface,
18
    StringPrimitive as Str
19
};
20
use Symfony\Component\{
21
    Config\Loader\Loader,
22
    Routing\RouteCollection,
23
    Routing\Route
24
};
25
26
final class RouteLoader extends Loader
27
{
28
    private $directories;
29
    private $routeFactory;
30
    private $imported = false;
31
32 3 View Code Duplication
    public function __construct(
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...
33
        MapInterface $directories,
34
        RouteFactory $routeFactory
35
    ) {
36
        if (
37 3
            (string) $directories->keyType() !== 'string' ||
38 3
            (string) $directories->valueType() !== Directory::class
39
        ) {
40
            throw new InvalidArgumentException;
41
        }
42
43 3
        $this->directories = $directories;
44 3
        $this->routeFactory = $routeFactory;
45 3
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 2
    public function load($resource, $type = null)
51
    {
52 2
        if ($this->imported === true) {
53 1
            throw new RouteLoaderLoadedMultipleTimesException;
54
        }
55
56 2
        $routes = new RouteCollection;
57
58 2
        foreach ($this->directories as $directory) {
59 2
            $routes->addCollection(
60 2
                $this->buildDirectoryRoutes($directory)
61
            );
62
        }
63
64 2
        $routes->add(
65 2
            'innmind_rest_server_capabilities',
66 2
            new Route(
67 2
                '/*',
68 2
                ['_controller' => 'innmind_rest_server.controller.capabilities:capabilitiesAction'],
69 2
                [],
70 2
                [],
71 2
                '',
72 2
                [],
73 2
                [MethodInterface::OPTIONS]
74
            )
75
        );
76
77 2
        $this->imported = true;
78
79 2
        return $routes;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 1
    public function supports($resource, $type = null)
86
    {
87 1
        return $type === 'innmind_rest';
88
    }
89
90 2
    private function buildDirectoryRoutes(Directory $directory): RouteCollection
91
    {
92
        return $directory
93 2
            ->flatten()
94 2
            ->reduce(
95 2
                new RouteCollection,
96
                function(RouteCollection $routes, string $name, HttpResource $definition) {
97 2
                    $routes->addCollection(
98 2
                        $this->buildResourceRoutes($name, $definition)
99
                    );
100
101 2
                    return $routes;
102 2
                }
103
            );
104
    }
105
106 2
    private function buildResourceRoutes(
107
        string $name,
108
        HttpResource $definition
109
    ): RouteCollection {
110 2
        return Action::all()
111 2
            ->reduce(
112 2
                new RouteCollection,
113 2
                function(RouteCollection $carry, string $action) use ($name, $definition) {
114
                    if (
115 2
                        $definition->options()->hasKey('actions') &&
116 2
                        !in_array($action, $definition->options()->get('actions'))
117
                    ) {
118
                        return $carry;
119
                    }
120
121 2
                    $carry->add(
122 2
                        $this->routeFactory->makeName($name, new Action($action)),
123 2
                        $this->routeFactory->makeRoute($name, new Action($action))
124
                    );
125
126 2
                    return $carry;
127 2
                }
128
            );
129
    }
130
}
131