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

application/modules/error/controllers/index.php (8 issues)

Labels
Severity
1
<?php
2
/**
3
 * Error controller
4
 * Send error headers and show simple page
5
 *
6
 * @author   Anton Shevchuk
7
 * @created  11.07.11 15:32
8
 */
9
10
/**
11
 * @namespace
12
 */
13
14
namespace Application;
15
16
use Bluz\Controller\Controller;
0 ignored issues
show
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...
17
use Bluz\Http\StatusCode;
0 ignored issues
show
The type Bluz\Http\StatusCode 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\Auth;
0 ignored issues
show
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...
19
use Bluz\Proxy\Layout;
20
use Bluz\Proxy\Logger;
0 ignored issues
show
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
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
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
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
25
/**
26
 * @accept ANY
27
 * @accept HTML
28
 * @accept JSON
29
 * @route  /error/{$code}
30
 *
31
 * @param int $code
32
 * @param \Exception $exception
33
 *
34
 * @return array
35
 */
36
return function ($code, $exception = null) {
37
    /**
38
     * @var Controller $this
39
     */
40
    // cast to valid HTTP error code
41
    // 500 - Internal Server Error
42 14
    $code = (StatusCode::CONTINUE <= $code && $code < 600) ? $code : StatusCode::INTERNAL_SERVER_ERROR;
43
    // use exception
44 14
    Response::setStatusCode($code);
45 14
    $exceptionMessage = '';
46
47 14
    if ($exception) {
48 14
        Logger::exception($exception);
49 14
        $exceptionMessage = $exception->getMessage();
50
    }
51
52
    // for debug mode you can use whoops
53
    /*
54
    if ($this->isDebug() && ($e = $this->getException())) {
55
        $whoops = new \Whoops\Run();
56
        if (PHP_SAPI === 'cli') {
57
            $whoops->pushHandler(new \Whoops\Handler\PlainTextHandler());
58
        } elseif (Request::checkAccept(['application/json'])) {
59
            $whoops->pushHandler(new \Whoops\Handler\JsonResponseHandler());
60
        } else {
61
            $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
62
        }
63
64
        $whoops->handleException($e);
65
        return false;
66
    }
67
    */
68
69 14
    switch ($code) {
70 14
        case 400:
71 1
            $error = __('Bad Request');
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
            $error = /** @scrutinizer ignore-call */ __('Bad Request');
Loading history...
72 1
            $message = $exceptionMessage ?? __('The server didn\'t understand the syntax of the request');
73 1
            break;
74 13
        case 401:
75 1
            $error = __('Unauthorized');
76 1
            $message = __('You are not authorized to view this page, please sign in');
77 1
            break;
78 12
        case 403:
79 6
            $error = __('Forbidden');
80 6
            if (Auth::getIdentity()) {
81 4
                $message = __('You don\'t have permissions to access this page');
82
            } else {
83 2
                $message = __('You don\'t have permissions, please sign in');
84
            }
85 6
            break;
86 6
        case 404:
87 4
            $error = __('Not Found');
88 4
            $message = __('The page you requested was not found');
89 4
            break;
90 2
        case 405:
91 1
            $error = __('Method Not Allowed');
92 1
            $message = __('The server is not support method `%s`', Request::getMethod());
93 1
            Response::setHeader('Allow', $exceptionMessage);
94 1
            break;
95 1
        case 406:
96
            $error = __('Not Acceptable');
97
            $message = __('The server is not acceptable generating content type described at `Accept` header');
98
            break;
99 1
        case 500:
100 1
            $error = __('Internal Server Error');
101 1
            $message = __('The server encountered an unexpected condition');
102 1
            break;
103
        case 501:
104
            $error = __('Not Implemented');
105
            $message = __('The server does not understand or does not support the HTTP method');
106
            break;
107
        case 503:
108
            $error = __('Service Unavailable');
109
            $message = __('The server is currently unable to handle the request due to a temporary overloading');
110
            Response::setHeader('Retry-After', '600');
111
            break;
112
        default:
113
            $error = __('Internal Server Error');
114
            $message = __('An unexpected error occurred with your request. Please try again later');
115
            break;
116
    }
117
118
    // check CLI or HTTP request
119 14
    if (Request::isHttp()) {
120
        // simple AJAX call, accept JSON
121
        if (Request::checkAccept([Request::TYPE_JSON])) {
122
            $this->useJson();
123
            Messages::addError($message);
124
            return [
125
                'code' => $code,
126
                'error' => $message
127
            ];
128
        }
129
    }
130
131 14
    Layout::title($error);
132
133
    return [
134 14
        'code' => $code,
135 14
        'error' => $error,
136 14
        'message' => $message,
137 14
        'exception' => $exception
138
    ];
139
};
140