Completed
Pull Request — master (#248)
by Anton
05:37
created

Bootstrap::finish()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 8
rs 9.4285
ccs 0
cts 5
cp 0
crap 12
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()) {
51 16
            if ($token = Request::getHeader('Bluz-Token')) {
52
                Auth\Table::getInstance()->authenticateToken($token);
53 16
            } elseif (!empty($_COOKIE['rToken']) && !empty($_COOKIE['rId'])) {
54
                // try to login
55
                try {
56
                    Auth\Table::getInstance()->authenticateCookie($_COOKIE['rId'], $_COOKIE['rToken']);
57
                } catch (AuthException $e) {
58
                    $this->getResponse()->setCookie('rId', '', 1, '/');
59
                    $this->getResponse()->setCookie('rToken', '', 1, '/');
60
                }
61
            }
62
        }
63
64 80
        parent::preDispatch($module, $controller, $params);
65 80
    }
66
67
    /**
68
     * {@inheritdoc}
69
     *
70
     * @param string $module
71
     * @param string $controller
72
     * @param array $params
73
     * @return void
74
     */
75 74
    protected function postDispatch($module, $controller, $params = array())
76
    {
77 74
        parent::postDispatch($module, $controller, $params);
78 74
    }
79
80
    /**
81
     * Denied access
82
     * @param ForbiddenException $exception
83
     * @return \Bluz\Controller\Controller|null
84
     */
85 2
    public function forbidden($exception)
86
    {
87 2
        if (AuthProxy::getIdentity()) {
88 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...
89
        } else {
90
            $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...
91
        }
92
93
        // for AJAX and API calls (over JSON)
94 2
        $jsonOrApi = Request::isXmlHttpRequest()
95 2
            || (Request::getAccept([Request::TYPE_HTML, Request::TYPE_JSON]) == Request::TYPE_JSON);
96
97
        // for guest, for requests
98 2
        if (!AuthProxy::getIdentity() && !$jsonOrApi) {
99
            // save URL to session and redirect make sense if presentation is null
100
            Session::set('rollback', Request::getUri()->__toString());
101
            // add error notice
102
            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...
103
            // redirect to Sign In page
104
            $url = Router::getUrl('users', 'signin');
105
            return $this->redirect($url);
106
        } else {
107 2
            return $this->error(new ForbiddenException($message, 403, $exception));
108
        }
109
    }
110
111
    /**
112
     * Render with debug headers
113
     * @return void
114
     */
115
    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...
116
    {
117
        if ($this->debugFlag && !headers_sent()) {
118
            $debugString = sprintf(
119
                "%fsec; %skb",
120
                microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'],
121
                ceil((memory_get_usage()/1024))
122
            );
123
            $debugString .= '; '. Request::getModule() .'/'. Request::getController();
124
125
            Response::setHeader('Bluz-Debug', $debugString);
126
127
            if ($info = Logger::get('info')) {
128
                Response::setHeader('Bluz-Bar', json_encode($info));
129
            } else {
130
                Response::setHeader('Bluz-Bar', '{"!":"Logger is disabled"}');
131
            }
132
        }
133
        parent::render();
134
    }
135
136
    /**
137
     * Finish it
138
     * @return void
139
     */
140
    public function finish()
141
    {
142
        if ($messages = Logger::get('error')) {
143
            foreach ($messages as $message) {
144
                errorLog(E_USER_ERROR, $message);
145
            }
146
        }
147
    }
148
}
149