Completed
Pull Request — master (#248)
by Anton
14:19 queued 04:07
created

Bootstrap::preDispatch()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 7.1941

Importance

Changes 0
Metric Value
cc 5
eloc 9
c 0
b 0
f 0
nc 3
nop 3
dl 0
loc 18
rs 8.8571
ccs 5
cts 9
cp 0.5556
crap 7.1941
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application;
11
12
use Bluz\Application\Application;
13
use Bluz\Application\Exception\ForbiddenException;
14
use Bluz\Auth\AuthException;
15
use Bluz\Proxy\Auth as AuthProxy;
16
use Bluz\Proxy\Layout;
17
use Bluz\Proxy\Logger;
18
use Bluz\Proxy\Messages;
19
use Bluz\Proxy\Response;
20
use Bluz\Proxy\Request;
21
use Bluz\Proxy\Router;
22
use Bluz\Proxy\Session;
23
use Bluz\Proxy\Translator;
24
25
/**
26
 * Bootstrap
27
 *
28
 * @category Application
29
 * @package  Bootstrap
30
 *
31
 * @author   Anton Shevchuk
32
 * @created  20.07.11 17:38
33
 */
34
class Bootstrap extends Application
35
{
36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @param string $module
40
     * @param string $controller
41
     * @param array $params
42
     * @return void
43
     */
44 80
    protected function preDispatch($module, $controller, $params = array())
0 ignored issues
show
Coding Style introduced by
preDispatch uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
45
    {
46
        // example of setup default title
47 80
        Layout::title("Bluz Skeleton");
48
49
        // apply "remember me" function
50 80
        if (!AuthProxy::getIdentity() && !empty($_COOKIE['rToken']) && !empty($_COOKIE['rId'])) {
51
            // try to login
52
            try {
53
                Auth\Table::getInstance()->authenticateCookie($_COOKIE['rId'], $_COOKIE['rToken']);
54
            } catch (AuthException $e) {
55
                $this->getResponse()->setCookie('rId', '', 1, '/');
56
                $this->getResponse()->setCookie('rToken', '', 1, '/');
57
            }
58
        }
59
60 80
        parent::preDispatch($module, $controller, $params);
61 80
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @param string $module
67
     * @param string $controller
68
     * @param array $params
69
     * @return void
70
     */
71 74
    protected function postDispatch($module, $controller, $params = array())
72
    {
73 74
        parent::postDispatch($module, $controller, $params);
74 74
    }
75
76
    /**
77
     * Denied access
78
     * @param ForbiddenException $exception
79
     * @return \Bluz\Controller\Controller|null
80
     */
81 2
    public function forbidden($exception)
82
    {
83 2
        if (AuthProxy::getIdentity()) {
84 2
            $message = Translator::translate("You don't have permissions to access this page");
0 ignored issues
show
Bug introduced by
The call to translate() misses a required argument $...$text.

This check looks for function calls that miss required arguments.

Loading history...
85
        } else {
86
            $message = Translator::translate("You don't have permissions, please sign in");
0 ignored issues
show
Bug introduced by
The call to translate() misses a required argument $...$text.

This check looks for function calls that miss required arguments.

Loading history...
87
        }
88
89
        // for AJAX and API calls (over JSON)
90 2
        $jsonOrApi = Request::isXmlHttpRequest()
91 2
            || (Request::getAccept([Request::TYPE_HTML, Request::TYPE_JSON]) == Request::TYPE_JSON);
92
93
        // for guest, for requests
94 2
        if (!AuthProxy::getIdentity() && !$jsonOrApi) {
95
            // save URL to session and redirect make sense if presentation is null
96
            Session::set('rollback', Request::getUri()->__toString());
97
            // add error notice
98
            Messages::addError($message);
0 ignored issues
show
Bug introduced by
The call to addError() misses a required argument $...$text.

This check looks for function calls that miss required arguments.

Loading history...
99
            // redirect to Sign In page
100
            $url = Router::getUrl('users', 'signin');
101
            return $this->redirect($url);
102
        } else {
103 2
            return $this->error(new ForbiddenException($message, 403, $exception));
104
        }
105
    }
106
107
    /**
108
     * Render with debug headers
109
     * @return void
110
     */
111
    public function render()
0 ignored issues
show
Coding Style introduced by
render uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
112
    {
113
        if ($this->debugFlag && !headers_sent()) {
114
            $debugString = sprintf(
115
                "%fsec; %skb",
116
                microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'],
117
                ceil((memory_get_usage()/1024))
118
            );
119
            $debugString .= '; '. Request::getModule() .'/'. Request::getController();
120
121
            Response::setHeader('Bluz-Debug', $debugString);
122
123
            if ($info = Logger::get('info')) {
124
                Response::setHeader('Bluz-Bar', json_encode($info));
125
            } else {
126
                Response::setHeader('Bluz-Bar', '{"!":"Logger is disabled"}');
127
            }
128
        }
129
        parent::render();
130
    }
131
132
    /**
133
     * Finish it
134
     * @return void
135
     */
136
    public function finish()
137
    {
138
        if ($messages = Logger::get('error')) {
139
            foreach ($messages as $message) {
140
                errorLog(E_USER_ERROR, $message);
141
            }
142
        }
143
    }
144
}
145