Failed Conditions
Pull Request — master (#321)
by Anton
23:35 queued 08:33
created

Bootstrap::sendInfoHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 10
nc 2
nop 0
dl 0
loc 15
ccs 0
cts 13
cp 0
c 0
b 0
f 0
cc 2
crap 6
rs 9.9332
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link      https://github.com/bluzphp/skeleton
5
 */
6
7
declare(strict_types=1);
8
9
namespace Application;
10
11
use Application\Auth;
12
use Bluz\Application\Application;
0 ignored issues
show
Bug introduced by
The type Bluz\Application\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Bluz\Auth\AuthException;
0 ignored issues
show
Bug introduced by
The type Bluz\Auth\AuthException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Bluz\Controller\Controller;
0 ignored issues
show
Bug introduced by
The type Bluz\Controller\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Bluz\Http\Exception\ForbiddenException;
0 ignored issues
show
Bug introduced by
The type Bluz\Http\Exception\ForbiddenException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Bluz\Http\Exception\RedirectException;
0 ignored issues
show
Bug introduced by
The type Bluz\Http\Exception\RedirectException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Bluz\Proxy\Auth as AuthProxy;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Bluz\Proxy\Config;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Bluz\Proxy\Layout;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Layout was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Bluz\Proxy\Logger;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Bluz\Proxy\Messages;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Messages was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Bluz\Proxy\Request;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Bluz\Proxy\Response;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Bluz\Proxy\Router;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Router was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Bluz\Proxy\Session;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Session was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Bluz\Proxy\Translator;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Translator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
/**
29
 * Bootstrap
30
 *
31
 * @category Application
32
 * @package  Bootstrap
33
 */
34
class Bootstrap extends Application
35
{
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function initTranslator(): void
40
    {
41
        $translator = new \Bluz\Translator\Translator();
0 ignored issues
show
Bug introduced by
The type Bluz\Translator\Translator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
        $translator->setOptions(Config::get('translator'));
43
44
        // supported locales
45
        // en_US, ru_RU, uk_UA
46
        $locales = [
47
            'en' => 'en_US',
48
            'ru' => 'ru_RU',
49
            'uk' => 'uk_UA',
50
        ];
51
52
        // try to check locale from cookies
53
        $locale = Request::getCookie('locale');
54
55
        // try to get locale from browser
56
        if (!$locale) {
57
            $languages = Request::getAcceptLanguage();
58
            foreach ($languages as $language => $priority) {
59
                if (array_key_exists($language, $locales)) {
60
                    $locale = $language;
61
                    break;
62
                }
63
            }
64
        }
65
66
        // normalize locale
67
        if (array_key_exists($locale, $locales)) {
68
            $translator->setLocale($locales[$locale]);
69
        }
70
71
        $translator->init();
72
73
        Translator::setInstance($translator);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @param Controller $controller
80
     *
81
     * @return void
82
     * @throws AuthException
83
     * @throws Exception
84
     * @throws \Bluz\Db\Exception\DbException
85
     * @throws \Bluz\Db\Exception\InvalidPrimaryKeyException
86
     */
87
    protected function preDispatch($controller): void
88
    {
89
        // example of setup default title
90
        Layout::title('Bluz Skeleton');
91
92
        if (!AuthProxy::getIdentity() && $controller->getModule() !== Router::getErrorModule()) {
93
            if ($token = Request::getCookie('Auth-Token')) {
94
                // try to login by token from cookies
95
                try {
96
                    Auth\Provider\Cookie::authenticate($token);
97
                } catch (AuthException $e) {
98
                    $this->getResponse()->setCookie('Auth-Token', '', 1, '/');
99
                }
100
            } elseif ($token = Request::getHeader('Auth-Token')) {
101
                // try to login by token from headers
102
                Auth\Provider\Token::authenticate($token);
103
            }
104
        }
105
        parent::preDispatch($controller);
106
    }
107
108
    /**
109
     * Denied access
110
     *
111
     * @param ForbiddenException $exception
112
     *
113
     * @return \Bluz\Controller\Controller|null
114
     */
115
    public function forbidden(ForbiddenException $exception): ?Controller
116
    {
117
        // for AJAX and API calls (over JSON)
118
        $jsonOrApi = Request::isXmlHttpRequest()
119
            || (Request::checkAccept([Request::TYPE_HTML, Request::TYPE_JSON]) === Request::TYPE_JSON);
120
121
        // for guest, for requests
122
        if (!$jsonOrApi && !AuthProxy::getIdentity()) {
123
            // save URL to session and redirect make sense if presentation is null
124
            Session::set('rollback', Request::getUri()->__toString());
125
            // add error notice
126
            Messages::addError('You don\'t have permissions, please sign in');
127
            // redirect to Sign In page
128
            $redirect = new RedirectException();
129
            $redirect->setUrl(Router::getUrl('users', 'signin'));
130
            return $this->redirect($redirect);
131
        }
132
        return $this->error($exception);
133
    }
134
135
    /**
136
     * Render with debug headers
137
     *
138
     * @return void
139
     */
140
    public function render(): void
141
    {
142
        Logger::info('app:render');
143
        Logger::info('app:files:' . count(get_included_files()));
144
        parent::render();
145
    }
146
147
    /**
148
     * Finish it
149
     *
150
     * @return void
151
     */
152
    public function end(): void
153
    {
154
        if ($errors = Logger::get('error')) {
155
            $this->sendErrors($errors);
156
        }
157
    }
158
159
    /**
160
     * sendErrorBody
161
     *
162
     * @param array $errors
163
     *
164
     * @return void
165
     */
166
    protected function sendErrors($errors): void
167
    {
168
        foreach ($errors as $message) {
169
            errorLog(new \ErrorException($message, 0, E_USER_ERROR));
170
        }
171
    }
172
}
173