ResolverSimple   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 12 4
A searchController() 0 18 3
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Route
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Route\Resolver;
16
17
use Phossa2\Route\Message\Message;
18
use Phossa2\Shared\Base\ObjectAbstract;
19
use Phossa2\Route\Exception\LogicException;
20
use Phossa2\Route\Interfaces\ResolverInterface;
21
22
/**
23
 * ResolverSimple
24
 *
25
 * Resolving ['ControllerName', 'ActionName'] into callable
26
 *
27
 * $handler = [new ControllerNameController(), 'ActionNameAction'];
28
 *
29
 * @package Phossa2\Route
30
 * @author  Hong Zhang <[email protected]>
31
 * @see     ObjectAbstract
32
 * @see     ResolverInterface
33
 * @version 2.1.0
34
 * @since   2.0.0 added
35
 * @since   2.1.0 updated
36
 */
37
class ResolverSimple extends ObjectAbstract implements ResolverInterface
38
{
39
    /**
40
     * @var    string
41
     * @access protected
42
     */
43
    protected $controller_suffix = 'Controller';
44
45
    /**
46
     * @var    string
47
     * @access protected
48
     */
49
    protected $action_suffix = 'Action';
50
51
    /**
52
     * Namespaces for controllers
53
     *
54
     * @var    string[]
55
     * @access protected
56
     */
57
    protected $namespaces = [];
58
59
    /**
60
     * @param  array $properties
61
     * @access public
62
     */
63
    public function __construct(array $properties = [])
64
    {
65
        $this->setProperties($properties);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function resolve($handler)/*# : callable */
72
    {
73
        if (is_callable($handler)) {
74
            return $handler;
75
        } elseif (is_array($handler) && isset($handler[1])) {
76
            return $this->searchController($handler[0], $handler[1]);
77
        }
78
        throw new LogicException(
79
            Message::get(Message::RTE_HANDLER_UNKNOWN, $handler),
80
            Message::RTE_HANDLER_UNKNOWN
81
        );
82
    }
83
84
    /**
85
     * Search controller base on the name
86
     *
87
     * @param  string $controller
88
     * @param  string $action
89
     * @return callable
90
     * @throws LogicException if not found
91
     * @access protected
92
     */
93
    protected function searchController(
94
        /*# string */ $controller,
95
        /*# string */ $action
96
    )/*# : callable */ {
97
        $controllerName = $controller . $this->controller_suffix;
98
        $actionName = $action . $this->action_suffix;
99
        foreach ($this->namespaces as $ns) {
100
            $class = $ns . '\\' . $controllerName;
101
            if (class_exists($class)) {
102
                $obj = new $class();
103
                return [$obj, $actionName];
104
            }
105
        }
106
        throw new LogicException(
107
            Message::get(Message::RTE_HANDLER_UNKNOWN, $controller),
108
            Message::RTE_HANDLER_UNKNOWN
109
        );
110
    }
111
}
112