Passed
Push — 2.x ( 321da8...312b44 )
by Quentin
08:47
created

Handler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 55
ccs 8
cts 8
cp 1
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTwillErrorView() 0 11 4
A invalidJson() 0 3 1
A getHttpExceptionView() 0 9 4
A unauthenticated() 0 5 2
1
<?php
2
3
namespace A17\Twill\Exceptions;
4
5
use Illuminate\Auth\AuthenticationException;
6
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
7
use Illuminate\Support\Facades\Route;
8
use Illuminate\Support\Facades\Request;
9
use Illuminate\Support\Str;
10
use Illuminate\Validation\ValidationException;
11
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
12
13
class Handler extends ExceptionHandler
14
{
15
    /**
16
     * Convert an authentication exception into a response.
17
     *
18
     * @param  \Illuminate\Http\Request  $request
19
     * @param  \Illuminate\Auth\AuthenticationException  $exception
20
     * @return \Symfony\Component\HttpFoundation\Response
21
     */
22
    protected function unauthenticated($request, AuthenticationException $exception)
23
    {
24
        return $request->expectsJson()
25
            ? response()->json(['message' => $exception->getMessage()], 401)
26
            : redirect()->guest($exception->redirectTo() ?? route('admin.login', Route::current()->parameters()));
27
    }
28
29
    /**
30
     * Get the view used to render HTTP exceptions.
31
     *
32
     * @param  \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface  $e
33
     * @return string
34
     */
35
    protected function getHttpExceptionView(HttpExceptionInterface $e)
36
    {
37
        $usesAdminPath = !empty(config('twill.admin_app_path'));
38
        $adminAppUrl = parse_url(config('twill.admin_app_url'));
39
40
        $isSubdomainAdmin = !$usesAdminPath && $adminAppUrl['host'] == Request::getHost();
41
        $isSubdirectoryAdmin = $usesAdminPath && Str::starts_with(Request::path(), config('twill.admin_app_path'));
42
43
        return $this->getTwillErrorView($e->getStatusCode(), !($isSubdomainAdmin || $isSubdirectoryAdmin));
44
    }
45
46
    /**
47
     * Get the Twill error view used to render a specified HTTP status code.
48
     *
49 54
     * @param  integer $statusCode
50
     * @return string
51
     */
52
    protected function getTwillErrorView($statusCode, $frontend = false)
53
    {
54
        if ($frontend) {
55
            $view = config('twill.frontend.views_path') . ".errors.$statusCode";
56
57 54
            return view()->exists($view)? $view : "errors::{$statusCode}";
58
        }
59 54
60 54
        $view = "admin.errors.$statusCode";
61 54
62 54
        return view()->exists($view) ? $view : "twill::errors.$statusCode";
63 54
    }
64 54
65
    protected function invalidJson($request, ValidationException $exception)
66
    {
67
        return response()->json($exception->errors(), $exception->status);
68
    }
69
}
70