Passed
Pull Request — 1.2 (#558)
by
unknown
09:10
created

Handler::render()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.5625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 19
ccs 9
cts 12
cp 0.75
crap 6.5625
rs 9.2222
1
<?php
2
3
namespace A17\Twill\Exceptions;
4
5
use Exception;
6
use Illuminate\Auth\Access\AuthorizationException;
7
use Illuminate\Auth\AuthenticationException;
8
use Illuminate\Config\Repository as Config;
9
use Illuminate\Contracts\Container\Container;
10
use Illuminate\Database\Eloquent\ModelNotFoundException;
11
use Illuminate\Foundation\Application;
12
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
13
use Illuminate\Routing\Redirector;
14
use Illuminate\Routing\ResponseFactory;
15
use Illuminate\Routing\UrlGenerator;
16
use Illuminate\Support\Str;
17
use Illuminate\Validation\ValidationException;
18
use Illuminate\View\Factory as ViewFactory;
19
use Symfony\Component\HttpKernel\Exception\HttpException;
20
use Illuminate\Http\Exceptions\HttpResponseException;
21
22
class Handler extends ExceptionHandler
23
{
24
    /**
25
     * Exceptions excluded from reporting.
26
     *
27
     * @var string[]
28
     */
29
    protected $dontReport = [
30
        AuthorizationException::class,
31
        HttpException::class,
32
        ModelNotFoundException::class,
33
        ValidationException::class,
34
    ];
35
36
    /**
37
     * @var bool
38
     */
39
    protected $isJsonOutputFormat = false;
40
41
    /**
42
     * @var Redirector
43
     */
44
    protected $redirector;
45
46
    /**
47
     * @var UrlGenerator
48
     */
49
    protected $urlGenerator;
50
51
    /**
52
     * @var Application
53
     */
54
    protected $app;
55
56
    /**
57
     * @var ViewFactory
58
     */
59
    protected $viewFactory;
60
61
    /**
62
     * @var ResponseFactory
63
     */
64
    protected $responseFactory;
65
66
    /**
67
     * @var Config
68
     */
69
    protected $config;
70
71
    /**
72
     * @param Container $container
73
     * @param Redirector $redirector
74
     * @param UrlGenerator $urlGenerator
75
     * @param Application $app
76
     * @param ViewFactory $viewFactory
77
     * @param ResponseFactory $responseFactory
78
     * @param Config $config
79
     */
80 50
    public function __construct(
81
        Container $container,
82
        Redirector $redirector,
83
        UrlGenerator $urlGenerator,
84
        Application $app,
85
        ViewFactory $viewFactory,
86
        ResponseFactory $responseFactory,
87
        Config $config
88
    ) {
89 50
        parent::__construct($container);
90
91 50
        $this->redirector = $redirector;
92 50
        $this->urlGenerator = $urlGenerator;
93 50
        $this->app = $app;
94 50
        $this->viewFactory = $viewFactory;
95 50
        $this->responseFactory = $responseFactory;
96 50
        $this->config = $config;
97 50
    }
98
99
    /**
100
     * @param Exception $e
101
     * @return mixed
102
     * @throws Exception
103
     */
104 8
    public function report(Exception $e)
105
    {
106 8
        return parent::report($e);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::report($e) targeting Illuminate\Foundation\Exceptions\Handler::report() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
107
    }
108
109
    /**
110
     * @param \Illuminate\Http\Request $request
111
     * @param Exception $e
112
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|string|Response
0 ignored issues
show
Bug introduced by
The type A17\Twill\Exceptions\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
113
     */
114 8
    public function render($request, Exception $e)
115
    {
116 8
        $e = $this->prepareException($e);
117
118 8
        $this->isJsonOutputFormat = $request->ajax() || $request->wantsJson();
119
120 8
        if ($e instanceof HttpResponseException) {
121
            return $e->getResponse();
122 8
        } elseif ($e instanceof AuthenticationException) {
123 1
            return $this->handleUnauthenticated($request, $e);
124 7
        } elseif ($e instanceof ValidationException) {
125
            return $this->convertValidationExceptionToResponse($e, $request);
126
        }
127
128 7
        if ($this->isJsonOutputFormat) {
129
            return $this->prepareJsonResponse($request, $e);
130
        }
131
132 7
        return $this->renderHttpExceptionWithView($request, $e);
133
    }
134
135
    /**
136
     * @param \Illuminate\Http\Request $request
137
     * @param \Exception $e
138
     * @return \Illuminate\Http\Response|Response
139
     */
140 7
    public function renderHttpExceptionWithView($request, $e)
141
    {
142 7
        if ($this->config->get('app.debug')) {
143
            return $this->convertExceptionToResponse($e);
144
        }
145
146 7
        $statusCode = $this->isHttpException($e) ? $e->getStatusCode() : 500;
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on Exception. It seems like you code against a sub-type of Exception such as Composer\Downloader\TransportException or Aws\Exception\AwsException or Symfony\Component\HttpKe...Exception\HttpException. ( Ignorable by Annotation )

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

146
        $statusCode = $this->isHttpException($e) ? $e->/** @scrutinizer ignore-call */ getStatusCode() : 500;
Loading history...
147 7
        $headers = $this->isHttpException($e) ? $e->getHeaders() : [];
0 ignored issues
show
Bug introduced by
The method getHeaders() does not exist on Exception. It seems like you code against a sub-type of Exception such as Composer\Downloader\TransportException or Symfony\Component\HttpKe...Exception\HttpException. ( Ignorable by Annotation )

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

147
        $headers = $this->isHttpException($e) ? $e->/** @scrutinizer ignore-call */ getHeaders() : [];
Loading history...
148
149 7
        $isSubdomainAdmin = empty($this->config->get('twill.admin_app_path')) && $request->getHost() == $this->config->get('twill.admin_app_url');
150 7
        $isSubdirectoryAdmin = !empty($this->config->get('twill.admin_app_path')) && Str::startsWith($request->path(), $this->config->get('twill.admin_app_path'));
151
152 7
        if ($isSubdomainAdmin || $isSubdirectoryAdmin) {
153 5
            $view = $this->viewFactory->exists("admin.errors.$statusCode") ? "admin.errors.$statusCode" : "twill::errors.$statusCode";
154
        } else {
155 2
            $view = $this->config->get('twill.frontend.views_path') . ".errors.{$statusCode}";
156
        }
157
158 7
        if ($this->viewFactory->exists($view)) {
159 5
            return $this->responseFactory->view($view, ['exception' => $e], $statusCode, $headers);
160
        }
161
162 2
        if ($this->isHttpException($e)) {
163 2
            return $this->renderHttpException($e);
0 ignored issues
show
Bug introduced by
$e of type Exception is incompatible with the type Symfony\Component\HttpKe...\HttpExceptionInterface expected by parameter $e of Illuminate\Foundation\Ex...::renderHttpException(). ( Ignorable by Annotation )

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

163
            return $this->renderHttpException(/** @scrutinizer ignore-type */ $e);
Loading history...
164
        }
165
166
        return parent::render($request, $e);
167
    }
168
169
    /**
170
     * @param \Illuminate\Http\Request $request
171
     * @param AuthenticationException $exception
172
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
173
     */
174 1
    protected function handleUnauthenticated($request, AuthenticationException $exception)
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed. ( Ignorable by Annotation )

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

174
    protected function handleUnauthenticated($request, /** @scrutinizer ignore-unused */ AuthenticationException $exception)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
    {
176 1
        if ($request->expectsJson()) {
177
            return $this->responseFactory->json(['error' => 'Unauthenticated.'], 401);
178
        }
179
180 1
        return $this->redirector->guest($this->urlGenerator->route('admin.login'));
181
    }
182
183
    /**
184
     * @param \Illuminate\Http\Request $request
185
     * @param ValidationException $exception
186
     * @return \Illuminate\Http\JsonResponse
187
     */
188
    protected function invalidJson($request, ValidationException $exception)
189
    {
190
        return $this->responseFactory->json($exception->errors(), $exception->status);
191
    }
192
}
193