RedirectDisplayer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 70
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A display() 0 4 1
A contentType() 0 4 1
A isVerbose() 0 4 1
A canDisplay() 0 6 3
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Exceptions\Displayers;
13
14
use Exception;
15
use GrahamCampbell\Exceptions\Displayers\DisplayerInterface;
16
use Illuminate\Http\Request;
17
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
18
19
class RedirectDisplayer implements DisplayerInterface
20
{
21
    /**
22
     * The request instance.
23
     *
24
     * @var \Illuminate\Http\Request
25
     */
26
    protected $request;
27
28
    /**
29
     * Create a new redirect displayer instance.
30
     *
31
     * @param \Illuminate\Http\Request $request
32
     */
33
    public function __construct(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
34
    {
35
        $this->request = $request;
36
    }
37
38
    /**
39
     * Get the error response associated with the given exception.
40
     *
41
     * @param \Exception $exception
42
     * @param string     $id
43
     * @param int        $code
44
     * @param string[]   $headers
45
     *
46
     * @return \Symfony\Component\HttpFoundation\Response
47
     */
48
    public function display(Exception $exception, $id, $code, array $headers)
49
    {
50
        return redirect()->guest('auth/login');
51
    }
52
53
    /**
54
     * Get the supported content type.
55
     *
56
     * @return string
57
     */
58
    public function contentType()
59
    {
60
        return 'text/html';
61
    }
62
63
    /**
64
     * Can we display the exception?
65
     *
66
     * @param \Exception $original
67
     * @param \Exception $transformed
68
     * @param int        $code
69
     *
70
     * @return bool
71
     */
72
    public function canDisplay(Exception $original, Exception $transformed, $code)
73
    {
74
        $redirect = $transformed instanceof HttpExceptionInterface && $transformed->getStatusCode() === 401;
75
76
        return $redirect && ! $this->request->is('api*');
77
    }
78
79
    /**
80
     * Do we provide verbose information about the exception?
81
     *
82
     * @return bool
83
     */
84
    public function isVerbose()
85
    {
86
        return false;
87
    }
88
}
89