Completed
Push — master ( b6e961...8692a9 )
by Abdelrahman
15:05 queued 12:57
created

ExceptionHandler::render()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 4
nop 2
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Fort\Handlers;
6
7
use Exception;
8
use Illuminate\Auth\AuthenticationException;
9
use Rinvex\Fort\Exceptions\GenericException;
10
use Illuminate\Foundation\Exceptions\Handler;
11
use Rinvex\Fort\Exceptions\AuthorizationException;
12
use Illuminate\Database\Eloquent\ModelNotFoundException;
13
14
class ExceptionHandler extends Handler
15
{
16
    /**
17
     * Render an exception into an HTTP response.
18
     *
19
     * @param \Illuminate\Http\Request $request
20
     * @param \Exception               $exception
21
     *
22
     * @return \Symfony\Component\HttpFoundation\Response
23
     */
24
    public function render($request, Exception $exception)
25
    {
26
        if ($exception instanceof ModelNotFoundException) {
27
            $model = str_replace('Contract', '', $exception->getModel());
28
            $isAdminarea = mb_strpos($request->route()->getName(), 'adminarea') !== false;
29
            $single = mb_strtolower(mb_substr($model, mb_strrpos($model, '\\') + 1));
30
            $plural = str_plural($single);
31
32
            return intend([
33
                'url' => $isAdminarea ? route("adminarea.{$plural}.index") : route('frontarea.home'),
34
                'with' => ['warning' => trans('messages.resource_not_found', ['resource' => $single, 'id' => $request->route()->parameter($single)])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 150 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
35
            ], 404);
36
        } elseif ($exception instanceof AuthorizationException) {
37
            return intend([
38
                'url' => '/',
39
                'with' => ['warning' => $exception->getMessage()],
40
            ], 403);
41
        } elseif ($exception instanceof GenericException) {
42
            return intend([
43
                'url' => $exception->getRedirection() ?? route('frontarea.home'),
44
                'withInput' => $exception->getInputs() ?? $request->all(),
45
                'with' => ['warning' => $exception->getMessage()],
46
            ], 422);
47
        }
48
49
        return parent::render($request, $exception);
50
    }
51
52
    /**
53
     * Convert an authentication exception into an unauthenticated response.
54
     *
55
     * @param \Illuminate\Http\Request                 $request
56
     * @param \Illuminate\Auth\AuthenticationException $exception
57
     *
58
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
59
     */
60
    protected function unauthenticated($request, AuthenticationException $exception)
61
    {
62
        return intend([
63
            'url' => route('frontarea.login'),
64
            'withErrors' => ['rinvex.fort.session.required' => trans('messages.auth.session.required')],
65
        ], 401);
66
    }
67
}
68