Completed
Push — develop ( d6be5d...69d261 )
by Baptiste
04:07
created

RouteFactory::makeRoute()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 52
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 41
cts 41
cp 1
rs 6.5703
c 0
b 0
f 0
cc 9
eloc 40
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 12
    public function makeName(string $path, Action $action): string
22
    {
23 12
        return (string) (new Str($path))
24 12
            ->prepend('innmind_rest_server.')
25 12
            ->append('.')
26 12
            ->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 24
    public function makeRoute(string $path, Action $action): Route
38
    {
39 24
        $name = $path;
40 24
        $path = (new Str($path))->replace('.', '/')->append('/');
41
42 24
        switch ((string) $action) {
43 24
            case Action::GET:
44 10
                $path = $path->append('{identity}');
45 22
            case Action::LIST:
46 12
                $method = MethodInterface::GET;
47 12
                break;
48 20
            case Action::CREATE:
49 10
                $method = MethodInterface::POST;
50 10
                break;
51 18
            case Action::UPDATE:
52 10
                $method = MethodInterface::PUT;
53 10
                $path = $path->append('{identity}');
54 10
                break;
55 16
            case Action::REMOVE:
56 10
                $method = MethodInterface::DELETE;
57 10
                $path = $path->append('{identity}');
58 10
                break;
59 14
            case Action::LINK:
60 10
                $method = MethodInterface::LINK;
61 10
                $path = $path->append('{identity}');
62 10
                break;
63 12
            case Action::UNLINK:
64 10
                $method = MethodInterface::UNLINK;
65 10
                $path = $path->append('{identity}');
66 10
                break;
67 10
            case Action::OPTIONS:
68 10
                $method = MethodInterface::OPTIONS;
69 10
                break;
70
        }
71
72 24
        return new Route(
73 24
            (string) $path,
74
            [
75 24
                '_innmind_resource' => $name,
76 24
                '_innmind_action' => (string) $action,
77 24
                '_controller' => sprintf(
78 24
                    'innmind_rest_server.controller.resource.%s:defaultAction',
79
                    $action
80
                ),
81
            ],
82 24
            [],
83 24
            [],
84 24
            '',
85 24
            [],
86
            $method
87
        );
88
    }
89
}
90