Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

ControllerDispatchInflector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 77
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A inflect() 0 5 1
A extractAttributes() 0 15 3
A createDispatch() 0 14 1
A filterName() 0 10 2
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Http\Dispatcher;
11
12
use Aura\Router\Route;
13
14
/**
15
 * Controller Class Inflector
16
 *
17
 * @package Slick\Mvc\Http\Dispatcher
18
 * @author  Filipe Silva <[email protected]>
19
 */
20
class ControllerDispatchInflector implements ControllerDispatchInflectorInterface
21
{
22
    /**
23
     * Returns the controller class name from provided route
24
     *
25
     * @param Route $route
26
     *
27
     * @return ControllerDispatch
28
     */
29
    public function inflect(Route $route)
30
    {
31
        $arguments = $this->extractAttributes($route);
32
        return $this->createDispatch($arguments);
33
    }
34
35
    /**
36
     * Get arguments form route attributes
37
     *
38
     * @param Route $route
39
     * @return array
40
     */
41
    private function extractAttributes(Route $route)
42
    {
43
        $arguments = [
44
            'namespace' => null,
45
            'controller' => null,
46
            'action' => null,
47
            'args' => []
48
        ];
49
        foreach (array_keys($arguments) as $name) {
50
            $arguments[$name] = array_key_exists($name, $route->attributes)
51
                ? $route->attributes[$name]
52
                : $arguments[$name];
53
        }
54
        return $arguments;
55
    }
56
57
    /**
58
     * Create a controller dispatch with provided arguments
59
     *
60
     * @param array $arguments
61
     *
62
     * @return ControllerDispatch|Object
63
     */
64
    private function createDispatch(array $arguments)
65
    {
66
        $arguments['controller'] = $this->filterName($arguments['controller']);
67
        $data = [
68
            'className' => ltrim(
69
                "{$arguments['namespace']}"."\\"."{$arguments['controller']}",
70
                "\\"
71
            ),
72
            'method' => $arguments['action'],
73
            'arguments' => $arguments['args']
74
        ];
75
        $reflection = new \ReflectionClass(ControllerDispatch::class);
76
        return $reflection->newInstanceArgs($data);
77
    }
78
79
    /**
80
     * Filters the controller class name
81
     *
82
     * @param string $name
83
     *
84
     * @return string
85
     */
86
    private function filterName($name)
87
    {
88
        $name = str_replace(['-', '_'], ' ', $name);
89
        $words = explode(' ', $name);
90
        $filtered = '';
91
        foreach ($words as $word) {
92
            $filtered .= ucfirst($word);
93
        }
94
        return $filtered;
95
    }
96
}