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

RouteFactory::makeRoute()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 56
Code Lines 44

Duplication

Lines 10
Ratio 17.86 %

Code Coverage

Tests 44
CRAP Score 9

Importance

Changes 0
Metric Value
dl 10
loc 56
ccs 44
cts 44
cp 1
rs 7.1584
c 0
b 0
f 0
cc 9
eloc 44
nc 9
nop 2
crap 9

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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