Passed
Push — 2.x ( 2d1b2e...fc5b16 )
by Quentin
16:12
created

Handler::invalidJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace A17\Twill\Exceptions;
4
5
use Illuminate\Auth\AuthenticationException;
6
use Illuminate\Config\Repository as Config;
7
use Illuminate\Contracts\Container\Container;
8
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
9
use Illuminate\Routing\Redirector;
10
use Illuminate\Routing\ResponseFactory;
11
use Illuminate\Routing\UrlGenerator;
12
use Illuminate\Validation\ValidationException;
13
use Illuminate\View\Factory as ViewFactory;
14
15
class Handler extends ExceptionHandler
16
{
17
18
    /**
19
     * @var Redirector
20
     */
21
    protected $redirector;
22
23
    /**
24
     * @var UrlGenerator
25
     */
26
    protected $urlGenerator;
27
28
    /**
29
     * @var ViewFactory
30
     */
31
    protected $viewFactory;
32
33
    /**
34
     * @var ResponseFactory
35
     */
36
    protected $responseFactory;
37
38
    /**
39
     * @var Config
40
     */
41
    protected $config;
42
43
    /**
44
     * @param Container $container
45
     * @param Redirector $redirector
46
     * @param UrlGenerator $urlGenerator
47
     * @param ResponseFactory $responseFactory
48
     */
49 50
    public function __construct(
50
        Container $container,
51
        Redirector $redirector,
52
        UrlGenerator $urlGenerator,
53
        ResponseFactory $responseFactory,
54
        Config $config,
55
        ViewFactory $viewFactory
0 ignored issues
show
Unused Code introduced by
The parameter $viewFactory 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

55
        /** @scrutinizer ignore-unused */ ViewFactory $viewFactory

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...
56
    ) {
57 50
        parent::__construct($container);
58
59 50
        $this->redirector = $redirector;
60 50
        $this->urlGenerator = $urlGenerator;
61 50
        $this->responseFactory = $responseFactory;
62 50
        $this->viewFactory = $responseFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $responseFactory of type Illuminate\Routing\ResponseFactory is incompatible with the declared type Illuminate\View\Factory of property $viewFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63 50
        $this->config = $config;
64 50
    }
65
66
    /**
67
     * Convert an authentication exception into a response.
68
     *
69
     * @param  \Illuminate\Http\Request  $request
70
     * @param  \Illuminate\Auth\AuthenticationException  $exception
71
     * @return \Symfony\Component\HttpFoundation\Response
72
     */
73 1
    protected function unauthenticated($request, AuthenticationException $exception)
74
    {
75 1
        return $request->expectsJson()
76
        ? $this->responseFactory->json(['message' => $exception->getMessage()], 401)
77 1
        : $this->redirector->guest($exception->redirectTo() ?? $this->urlGenerator->route('admin.login'));
78
    }
79
80
    /**
81
     * Get the Twill error view used to render a specified HTTP status code.
82
     *
83
     * @param  integer $statusCode
84
     * @return string
85
     */
86
    protected function getTwillErrorView($statusCode, $frontend = false)
87
    {
88
        if ($frontend) {
89
            return $this->config->get('twill.frontend.views_path') . ".errors.{$statusCode}";
90
        }
91
92
        return $this->viewFactory->exists("admin.errors.$statusCode") ? "admin.errors.$statusCode" : "twill::errors.$statusCode";
93
    }
94
95
    protected function invalidJson($request, ValidationException $exception)
96
    {
97
        return response()->json($exception->errors(), $exception->status);
98
    }
99
}
100