Completed
Push — dev-master ( c33e43...966d5f )
by Derek Stephen
02:51
created

Dispatcher   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 98.95%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 22
lcom 1
cbo 8
dl 0
loc 193
ccs 94
cts 95
cp 0.9895
rs 10
c 4
b 1
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A checkNavigator() 0 16 3
A checkControllerExists() 0 4 1
A checkActionExists() 0 4 1
A getResponseBody() 0 21 4
A fireCannons() 0 23 2
A setHeaders() 0 6 2
A plunderEnemyShip() 0 8 1
A sinkingShip() 0 14 2
A templateCheck() 0 11 3
A setNotFound() 0 8 2
1
<?php
2
3
namespace Bone\Mvc;
4
5
use Bone\Filter;
6
use Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Bone\Mvc\Exception.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use ReflectionClass;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Zend\Diactoros\Response;
11
use Zend\Diactoros\Response\SapiEmitter;
12
use Zend\Diactoros\Stream;
13
14
/**
15
 * Class Dispatcher
16
 * @package Bone\Mvc
17
 */
18
class Dispatcher
19
{
20
    // Garrrr! An arrrray!
21
    private $config = array();
22
23
    /** @var RequestInterface $request */
24
    private $request;
25
26
    /** @var Controller */
27
    private $controller;
28
29
    /** @var ResponseInterface $response */
30
    private $response;
31
32
33 10
    public function __construct(RequestInterface $request, ResponseInterface $response)
34 10
    {
35 10
        $this->request = $request;
36 10
        $this->response = $response;
37
38 10
        $router = new Router($request);
39 10
        $router->parseRoute();
40
41
        // what controller be we talkin' about?
42 10
        $filtered = Filter::filterString($router->getController(), 'DashToCamelCase');
43 10
        $this->config['controller_name'] = '\App\Controller\\' . ucwords($filtered) . 'Controller';
44
45
        // whit be yer action ?
46 10
        $filtered = Filter::filterString($router->getAction(), 'DashToCamelCase');
47 10
        $this->config['action_name'] = $filtered . 'Action';
48 10
        $this->config['controller'] = $router->getController();
49 10
        $this->config['action'] = $router->getAction();
50 10
    }
51
52
53
    /**
54
     *  Gaaarrr! Check the Navigator be readin' the map!
55
     */
56 2
    public function checkNavigator()
57 2
    {
58
        // can we find th' darned controller?
59 1
        if (!$this->checkControllerExists()) {
60 1
            $this->setNotFound();
61 1
            return;
62
        }
63
64
        // gaaarr! there be the controller!
65 1
        $this->controller = new $this->config['controller_name']($this->request);
66
67
        // where's the bloody action?
68 1
        if (!$this->checkActionExists()) {
69 1
            $this->setNotFound();
70 1
        }
71 1
    }
72
73
74
    /**
75
     * @return bool
76
     */
77 2
    private function checkControllerExists()
78 2
    {
79 2
        return class_exists($this->config['controller_name']);
80
    }
81
82
83
    /**
84
     * @return bool
85
     */
86 2
    private function checkActionExists()
87 2
    {
88 2
        return method_exists($this->controller, $this->config['action_name']);
89
    }
90
91
92
    /**
93
     * @return string
94
     * @throws \Exception
95
     */
96 3
    private function getResponseBody()
97 3
    {
98
        /** @var \stdClass $view_vars */
99 3
        $view_vars = $this->controller->view;
100
101 3
        $response_body = $this->controller->getBody();
102
103 3
        if ($this->controller->hasViewEnabled()) {
104 2
            $view = $this->config['controller'] . '/' . $this->config['action'] . '.twig';
105
            try {
106 2
                $response_body = $this->controller->getTwig()->render($view, (array)$view_vars);
107 2
            } catch (Exception $e) {
108 1
                throw $e;
109
            }
110 2
        }
111
112 3
        if ($this->controller->hasLayoutEnabled()) {
113 2
            $response_body = $this->templateCheck($this->controller, $response_body);
114 2
        }
115 3
        return $response_body;
116
    }
117
118
119
    /**
120
     *
121
     */
122 2
    public function fireCannons()
123 2
    {
124
        try {
125
            // Where be the navigator? Be we on course?
126 1
            $this->checkNavigator();
127
128
            // boom! direct hit Cap'n! Be gettin' the booty!
129 1
            $this->plunderEnemyShip();
130
131
            // show th' cap'n th' booty
132 1
            $booty = $this->getResponseBody();
133 1
        } catch (Exception $e) {
134 1
            $booty = $this->sinkingShip($e);
135
        }
136
137 1
        $this->response->getBody()->write($booty);
138
139
        // report back to th' cap'n
140 1
        $this->setHeaders();
141
142 1
        $emitter = new SapiEmitter();
143 1
        return $emitter->emit($this->response);
144
    }
145
146 1
    private function setHeaders()
147 1
    {
148 1
        foreach ($this->controller->getHeaders() as $key => $value) {
149
            $this->response = $this->response->withHeader($key, $value);
150 1
        }
151 1
    }
152
153
154 2
    private function plunderEnemyShip()
155 2
    {
156
        // run th' controller action
157 2
        $action = $this->config['action_name'];
158 2
        $this->controller->init();
159 2
        $this->controller->$action();
160 2
        $this->controller->postDispatch();
161 2
    }
162
163
164 2
    public function sinkingShip($e)
165 2
    {
166
//        $this->request->setParam('error', $e);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
167 1
        $this->controller = class_exists('\App\Controller\ErrorController') ? new \App\Controller\ErrorController($this->request) : new Controller($this->request);
0 ignored issues
show
Documentation Bug introduced by
It seems like class_exists('\\App\\Con...troller($this->request) can also be of type object<App\Controller\ErrorController>. However, the property $controller is declared as type object<Bone\Mvc\Controller>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
168
169 1
        $reflection = new ReflectionClass(get_class($this->controller));
170 1
        $method = $reflection->getMethod('errorAction');
171 1
        $method->setAccessible(true);
172 1
        $method->invokeArgs($this->controller, []);
173 1
        $this->controller->error = $e;
174 1
        $this->config['controller'] = 'error';
175 1
        $this->config['action'] = 'error';
176 1
        return $this->getResponseBody();
177
    }
178
179
180
    /**
181
     * @param Controller $controller
182
     * @param string $content
183
     * @return string
184
     */
185 3
    private function templateCheck($controller, $content)
186 3
    {
187 3
        $response_body = '';
188
        //check we be usin' th' templates in th' config
189 3
        $templates = Registry::ahoy()->get('templates');
190 3
        $template = ($templates != null) ? $templates[0] : null;
191 3
        if ($template) {
192 3
            $response_body = $controller->getTwig()->render('layouts/' . $template . '.twig', array('content' => $content));
193 3
        }
194 3
        return $response_body;
195
    }
196
197
198
    /**
199
     * Sets controller to error and action to not found
200
     * @return void
201
     */
202 2
    private function setNotFound()
203 2
    {
204 2
        $this->config['controller_name'] = class_exists('\App\Controller\ErrorController') ? '\App\Controller\ErrorController' : '\Bone\Mvc\Controller';
205 2
        $this->config['action_name'] = 'notFoundAction';
206 2
        $this->config['controller'] = 'error';
207 2
        $this->config['action'] = 'not-found';
208 2
        $this->controller = new $this->config['controller_name']($this->request);
209 2
    }
210
}
211