Completed
Push — master ( 98a062...441031 )
by Ryan
10:10
created

ExceptionHandler::render()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 7

Duplication

Lines 7
Ratio 38.89 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 7
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Exception;
2
3
use Exception;
4
use GrahamCampbell\Exceptions\NewExceptionHandler;
5
use Illuminate\Auth\AuthenticationException;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
9
/**
10
 * Class ExceptionHandler
11
 *
12
 * @link   http://pyrocms.com/
13
 * @author PyroCMS, Inc. <[email protected]>
14
 * @author Ryan Thompson <[email protected]>
15
 */
16
class ExceptionHandler extends NewExceptionHandler
17
{
18
19
    /**
20
     * A list of the exception types that should not be reported.
21
     *
22
     * @var array
23
     */
24
    protected $dontReport = [
25
        \Illuminate\Auth\AuthenticationException::class,
26
        \Illuminate\Auth\Access\AuthorizationException::class,
27
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
28
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
29
        \Illuminate\Session\TokenMismatchException::class,
30
        \Illuminate\Validation\ValidationException::class,
31
    ];
32
33
    /**
34
     * Render an exception into an HTTP response.
35
     *
36
     * @param  Request   $request
37
     * @param  Exception $e
38
     * @return Response
39
     */
40
    public function render($request, Exception $e)
41
    {
42
        /**
43
         * Have to catch this for some reason.
44
         * Not sure why our handler passes this.
45
         *
46
         * @todo: Clean up
47
         */
48 View Code Duplication
        if ($e instanceof AuthenticationException) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            if ($request->segment(1) === 'admin') {
50
                return redirect()->guest('admin/login');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return redirect()->guest('admin/login'); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by Anomaly\Streams\Platform...xceptionHandler::render of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
51
            } else {
52
                return redirect()->guest('login');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return redirect()->guest('login'); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by Anomaly\Streams\Platform...xceptionHandler::render of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
53
            }
54
        }
55
56
        return parent::render($request, $e);
57
    }
58
59
    /**
60
     * Convert an authentication exception into an unauthenticated response.
61
     *
62
     * @param  \Illuminate\Http\Request                 $request
63
     * @param  \Illuminate\Auth\AuthenticationException $exception
64
     * @return \Illuminate\Http\Response
65
     */
66
    protected function unauthenticated($request, AuthenticationException $exception)
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68
        if ($request->expectsJson()) {
69
            return response()->json(['error' => 'Unauthenticated.'], 401);
70
        }
71
72 View Code Duplication
        if ($request->segment(1) === 'admin') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
            return redirect()->guest('admin/login');
74
        } else {
75
            return redirect()->guest('login');
76
        }
77
    }
78
}
79