Completed
Push — director-middleware ( f3cb14...77371e )
by Sam
09:01
created

getAuthenticationHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Security;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Control\HTTPResponse;
7
use SilverStripe\Control\HTTPResponse_Exception;
8
use SilverStripe\Control\HTTPMiddleware;
9
use SilverStripe\Core\Config\Configurable;
10
use SilverStripe\ORM\ValidationException;
11
12
class AuthenticationMiddleware implements HTTPMiddleware
13
{
14
    use Configurable;
15
16
    /**
17
     * @var AuthenticationHandler
18
     */
19
    protected $authenticationHandler;
20
21
    /**
22
     * @return AuthenticationHandler
23
     */
24
    public function getAuthenticationHandler()
25
    {
26
        return $this->authenticationHandler;
27
    }
28
29
    /**
30
     * @param AuthenticationHandler $authenticationHandler
31
     * @return $this
32
     */
33
    public function setAuthenticationHandler(AuthenticationHandler $authenticationHandler)
34
    {
35
        $this->authenticationHandler = $authenticationHandler;
36
        return $this;
37
    }
38
39
    /**
40
     * Identify the current user from the request
41
     *
42
     * @param HTTPRequest $request
43
     * @return bool|void
44
     * @throws HTTPResponse_Exception
45
     */
46
    public function process(HTTPRequest $request, callable $delegate)
47
    {
48
        if (Security::database_is_ready()) {
49
            try {
50
                $this
51
                    ->getAuthenticationHandler()
52
                    ->authenticateRequest($request);
53
            } catch (ValidationException $e) {
54
                return new HTTPResponse(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \SilverStripe...$e->getMessage(), 400); (SilverStripe\Control\HTTPResponse) is incompatible with the return type documented by SilverStripe\Security\Au...tionMiddleware::process of type boolean|null.

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...
55
                    "Bad log-in details: " . $e->getMessage(),
56
                    400
57
                );
58
            }
59
        }
60
61
        return $delegate($request);
62
    }
63
    }
64