Completed
Push — master ( a73fad...f8ec74 )
by Baptiste
02:31
created

RouteFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 12.05 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 2
dl 10
loc 83
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A makeName() 0 7 1
B makeRoute() 10 56 9

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\Server\Action;
7
use Innmind\Http\Message\MethodInterface;
8
use Innmind\Immutable\StringPrimitive as Str;
9
use Symfony\Component\Routing\Route;
10
11
final class RouteFactory
12
{
13
    /**
14
     * Make a route name
15
     *
16
     * @param string $path Path to resource definition
17
     * @param Action $action
18
     *
19
     * @return string
20
     */
21 5
    public function makeName(string $path, Action $action): string
22
    {
23 5
        return (string) (new Str($path))
24 5
            ->prepend('innmind_rest_server.')
25 5
            ->append('.')
26 5
            ->append((string) $action);
27
    }
28
29
    /**
30
     * Make a route
31
     *
32
     * @param string $path Path to resource definition
33
     * @param Action $action
34
     *
35
     * @return Route
36
     */
37 13
    public function makeRoute(string $path, Action $action): Route
38
    {
39 13
        $name = $path;
40 13
        $path = (new Str($path))->replace('.', '/')->append('/');
41 13
        $condition = '';
42
43 13
        switch ((string) $action) {
44 13
            case Action::GET:
45 5
                $path = $path->append('{identity}');
46 12
            case Action::LIST:
47 6
                $method = MethodInterface::GET;
48 6
                break;
49 11
            case Action::CREATE:
50 5
                $method = MethodInterface::POST;
51 5
                break;
52 10
            case Action::UPDATE:
53 5
                $method = MethodInterface::PUT;
54 5
                $path = $path->append('{identity}');
55 5
                break;
56 9
            case Action::REMOVE:
57 5
                $method = MethodInterface::DELETE;
58 5
                $path = $path->append('{identity}');
59 5
                break;
60 8 View Code Duplication
            case Action::LINK:
61 5
                $method = MethodInterface::LINK;
62 5
                $path = $path->append('{identity}');
63 5
                $condition = 'request.headers.has(\'Link\')';
64 5
                break;
65 7 View Code Duplication
            case Action::UNLINK:
66 5
                $method = MethodInterface::UNLINK;
67 5
                $path = $path->append('{identity}');
68 5
                $condition = 'request.headers.has(\'Link\')';
69 5
                break;
70 6
            case Action::OPTIONS:
71 6
                $method = MethodInterface::OPTIONS;
72 6
                break;
73
        }
74
75 13
        return new Route(
76 13
            (string) $path,
77
            [
78 13
                '_innmind_resource' => $name,
79 13
                '_innmind_action' => (string) $action,
80 13
                '_controller' => sprintf(
81 13
                    'innmind_rest_server.controller.resource.%s:defaultAction',
82
                    $action
83
                ),
84
            ],
85 13
            [],
86 13
            [],
87 13
            '',
88 13
            [],
89
            $method,
90
            $condition
91
        );
92
    }
93
}
94