RouteFactory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 79
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A makeName() 0 7 1
C makeRoute() 0 52 9
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\Method;
8
use Innmind\Immutable\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
        switch ($action) {
43 24
            case Action::get():
44 10
                $path = $path->append('{identity}');
45 22
            case Action::list():
46 12
                $method = Method::GET;
47 12
                break;
48 20
            case Action::create():
49 10
                $method = Method::POST;
50 10
                break;
51 18
            case Action::update():
52 10
                $method = Method::PUT;
53 10
                $path = $path->append('{identity}');
54 10
                break;
55 16
            case Action::remove():
56 10
                $method = Method::DELETE;
57 10
                $path = $path->append('{identity}');
58 10
                break;
59 14
            case Action::link():
60 10
                $method = Method::LINK;
61 10
                $path = $path->append('{identity}');
62 10
                break;
63 12
            case Action::unlink():
64 10
                $method = Method::UNLINK;
65 10
                $path = $path->append('{identity}');
66 10
                break;
67 10
            case Action::options():
68 10
                $method = Method::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 24
                    $action
80
                ),
81
            ],
82 24
            [],
83 24
            [],
84 24
            '',
85 24
            [],
86 24
            $method
87
        );
88
    }
89
}
90