Completed
Pull Request — master (#247)
by
unknown
09:38
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\Layout;
16
use Bluz\Proxy\Logger;
17
use Bluz\Proxy\Messages;
18
use Bluz\Proxy\Response;
19
use Bluz\Proxy\Request;
20
use Bluz\Proxy\Router;
21
use Bluz\Proxy\Session;
22
use Bluz\Proxy\Translator;
23
24
/**
25
 * Bootstrap
26
 *
27
 * @category Application
28
 * @package  Bootstrap
29
 *
30
 * @author   Anton Shevchuk
31
 * @created  20.07.11 17:38
32
 */
33
class Bootstrap extends Application
34
{
35
    /**
36
     * {@inheritdoc}
37
     *
38
     * @param string $module
39
     * @param string $controller
40
     * @param array $params
41
     * @return void
42
     */
43 84
    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...
44
    {
45
        // example of setup default title
46 84
        Layout::title("Bluz Skeleton");
47
48
        // apply "remember me" function
49 84
        if (!$this->user() && !empty($_COOKIE['rToken']) && !empty($_COOKIE['rId'])) {
50
            // try to login
51
            try {
52
                Auth\Table::getInstance()->authenticateCookie($_COOKIE['rId'], $_COOKIE['rToken']);
53
            } catch (AuthException $e) {
54
                $this->getResponse()->setCookie('rId', '', 1, '/');
55
                $this->getResponse()->setCookie('rToken', '', 1, '/');
56
            }
57
        }
58
59 84
        parent::preDispatch($module, $controller, $params);
60 81
    }
61
62
    /**
63
     * {@inheritdoc}
64
     *
65
     * @param string $module
66
     * @param string $controller
67
     * @param array $params
68
     * @return void
69
     */
70 76
    protected function postDispatch($module, $controller, $params = array())
71
    {
72 76
        parent::postDispatch($module, $controller, $params);
73 76
    }
74
75
    /**
76
     * Denied access
77
     * @throws ForbiddenException
78
     * @return void
79
     */
80 5
    public function denied()
81
    {
82 5
        if ($this->user()) {
83 3
            $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...
84
        } else {
85 2
            $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...
86
        }
87
88
        // for guest, for requests
89 5
        if (!$this->user() && !Request::isXmlHttpRequest()) {
90
            // save URL to session and redirect make sense if presentation is null
91 2
            Session::set('rollback', Request::getRequestUri());
92
            // add error notice
93 2
            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...
94
            // redirect to Sign In page
95 2
            $this->redirectTo('users', 'signin');
96
        } else {
97 3
            throw new ForbiddenException($message);
98
        }
99
    }
100
101
    /**
102
     * Render with debug headers
103
     * @return void
104
     */
105
    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...
106
    {
107
        if ($this->debugFlag && !headers_sent()) {
108
            $debugString = sprintf(
109
                "%fsec; %skb",
110
                microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'],
111
                ceil((memory_get_usage()/1024))
112
            );
113
            $debugString .= '; '. Request::getModule() .'/'. Request::getController();
114
115
            Response::setHeader('Bluz-Debug', $debugString);
116
117
            if ($info = Logger::get('info')) {
118
                Response::setHeader('Bluz-Bar', json_encode($info));
119
            } else {
120
                Response::setHeader('Bluz-Bar', '{"!":"Logger is disabled"}');
121
            }
122
        }
123
        parent::render();
124
    }
125
126
    /**
127
     * Finish it
128
     * @return void
129
     */
130
    public function finish()
131
    {
132
        if ($messages = Logger::get('error')) {
133
            foreach ($messages as $message) {
134
                errorLog(E_USER_ERROR, $message);
135
            }
136
        }
137
    }
138
}
139