Completed
Push — master ( 0d6d09...6d9e57 )
by Dan
02:38
created

ClassHandler::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Ds\Router\Handler;
4
5
use Ds\Router\Interfaces\RouteHandlerInterface;
6
use Psr\Http\Message\ResponseInterface;
7
8
9
class ClassHandler extends Handler implements RouteHandlerInterface
10
{
11
12
    /**
13
     * @var string
14
     */
15
    protected $controller;
16
17
    /**
18
     * @var string
19
     */
20
    protected $method;
21
22
    /**
23
     * @param string $controller
24
     * @param string $method
25
     * @return ClassHandler
26
     */
27
    public static function add($controller, $method){
28
        return new ClassHandler($controller, $method);
29
    }
30
31
    /**
32
     *
33
     * Class Handler Construct.
34
     * Controller Name and Method name provided as FQN.
35
     *
36
     * @param string $controller
37
     * @param string $method
38
     *
39
     * @throws \Exception
40
     */
41
    public function __construct($controller = '', $method = '')
42
    {
43
        $this->controller = $controller;
44
        $this->method = $method;
45
        $this->preResponse = "{$controller}::{$method}";
46
    }
47
48
    /**
49
     * Resolve Controller and inject Arguments into construct. Normally this this the View.
50
     * @param array ...$arguments
51
     * @return $this
52
     * @throws \Exception
53
     */
54
    public function resolve(...$arguments){
55
56
57
        $controllerMethod = explode('::',$this->getPreResponse());
58
59
        $this->controller = isset($controllerMethod[0]) ? $controllerMethod[0] : $this->controller;
60
        $this->method = isset($controllerMethod[1]) ? $controllerMethod[1] : $this->method;
61
62
        try{
63
            $this->resolveController( $this->controller);
64
            $this->resolveMethod( $this->method );
65
            $this->resolved = new $this->controller(...$arguments);
66
        }catch(\Exception $e){
67
            throw new \Exception($e->getMessage());
68
        }
69
        return $this;
70
    }
71
72
73
    /**
74
     * Returns Response Object from Handler.
75
     * @param array $vars
76
     *
77
     * @return ResponseInterface
78
     * @throws \Exception
79
     */
80
    public function getResponse($vars = [] ){
81
82
        if ($this->resolved === null){
83
            throw new \Exception("Handler must be resolved before calling getContents().");
84
        }
85
86
        $controller = $this->resolved;
87
        $method = $this->method;
88
89
        return $controller->$method($vars);
90
    }
91
92
    /**
93
     * Check that controller exists.
94
     * @param $controller
95
     *
96
     * @return bool
97
     * @throws \Exception controller:{controller} not found
98
     */
99
    private function resolveController($controller){
100
101
        if ( !class_exists($controller)){
102
            throw new \Exception("Controller {$controller} not found");
103
        }
104
        return true;
105
    }
106
107
    /**
108
     * Check that the method exists.
109
     * @param $method
110
     *
111
     * @return bool
112
     * @throws \Exception
113
     */
114
    private function resolveMethod($method){
115
        if (method_exists($this->controller,$method)){
116
            return true;
117
        }
118
        throw new \Exception("method: {$this->controller}::{$method} not found");
119
    }
120
}
121