Passed
Push — master ( 6f10be...481678 )
by Quentin
10:51 queued 04:50
created

Handler::report()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\View\Factory as ViewFactory;
13
14
class Handler extends ExceptionHandler
15
{
16
17
    /**
18
     * @var Redirector
19
     */
20
    protected $redirector;
21
22
    /**
23
     * @var UrlGenerator
24
     */
25
    protected $urlGenerator;
26
27
    /**
28
     * @var ViewFactory
29
     */
30
    protected $viewFactory;
31
32
    /**
33
     * @var ResponseFactory
34
     */
35
    protected $responseFactory;
36
37
    /**
38
     * @var Config
39
     */
40
    protected $config;
41
42
    /**
43
     * @param Container $container
44
     * @param Redirector $redirector
45
     * @param UrlGenerator $urlGenerator
46
     * @param ResponseFactory $responseFactory
47
     */
48 50
    public function __construct(
49
        Container $container,
50
        Redirector $redirector,
51
        UrlGenerator $urlGenerator,
52
        ResponseFactory $responseFactory,
53
        Config $config,
54
        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

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