Completed
Push — master ( edb43e...425779 )
by Abdelrahman
10:35
created

ExceptionHandler::render()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 4
nop 2
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
declare(strict_types=1);
17
18
namespace Rinvex\Fort\Handlers;
19
20
use Exception;
21
use Illuminate\Auth\AuthenticationException;
22
use Rinvex\Fort\Exceptions\AuthorizationException;
23
use App\Exceptions\Handler as BaseExceptionHandler;
24
use Rinvex\Fort\Exceptions\InvalidPersistenceException;
25
use Illuminate\Database\Eloquent\ModelNotFoundException;
26
27
class ExceptionHandler extends BaseExceptionHandler
28
{
29
    /**
30
     * Render an exception into an HTTP response.
31
     *
32
     * @param \Illuminate\Http\Request $request
33
     * @param \Exception               $exception
34
     *
35
     * @return \Illuminate\Http\Response
36
     */
37
    public function render($request, Exception $exception)
38
    {
39
        if ($exception instanceof InvalidPersistenceException) {
40
            return intend([
41
                'route' => 'frontend.auth.login',
42
                'withErrors' => ['rinvex.fort.session.expired' => trans('messages.auth.session.expired')],
43
            ], 401);
44
        } elseif ($exception instanceof ModelNotFoundException) {
45
            $single = mb_strtolower(trim(mb_strrchr($exception->getModel(), '\\'), '\\'));
46
            $plural = str_plural($single);
47
48
            return intend([
49
                'route' => 'backend.'.$plural.'.index',
50
                'withErrors' => ['rinvex.fort.'.$single.'.not_found' => trans('messages.'.$single.'.not_found')],
51
            ]);
52
        } elseif ($exception instanceof AuthorizationException) {
53
            return intend([
54
                'url' => '/',
55
                'withErrors' => ['rinvex.fort.unauthorized' => $exception->getMessage()],
56
            ], 403);
57
        }
58
59
        return parent::render($request, $exception);
60
    }
61
62
    /**
63
     * Convert an authentication exception into an unauthenticated response.
64
     *
65
     * @param \Illuminate\Http\Request                 $request
66
     * @param \Illuminate\Auth\AuthenticationException $exception
67
     *
68
     * @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...
69
     */
70
    protected function unauthenticated($request, AuthenticationException $exception)
71
    {
72
        return intend([
73
            'route' => 'frontend.auth.login',
74
            'withErrors' => ['rinvex.fort.session.required' => trans('messages.auth.session.required')],
75
        ], 401);
76
    }
77
}
78