Completed
Push — master ( d2c9b2...2cde9b )
by
unknown
08:05 queued 04:31
created

Psr7Authenticate::getUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
/**
13
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
14
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 *
16
 * Licensed under The MIT License
17
 * For full copyright and license information, please see the LICENSE.txt
18
 * Redistributions of files must retain the above copyright notice.
19
 *
20
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
21
 * @link          http://cakephp.org CakePHP(tm) Project
22
 * @since         0.10.0
23
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
24
 */
25
26
namespace CakeDC\Api\Service\Auth\Authenticate;
27
28
use Cake\Datasource\EntityInterface;
29
use Cake\Http\Response;
30
use Cake\Http\ServerRequest;
31
32
/**
33
 * Class FormAuthenticate.
34
 */
35
class Psr7Authenticate extends BaseAuthenticate
36
{
37
38
    /**
39
     * Authenticates the identity contained in a request. Will use the `config.userModel`, and `config.fields`
40
     * to find POST data that is used to find a matching record in the `config.userModel`. Will return false if
41
     * there is no post data, either username or password is missing, or if the scope conditions have not been met.
42
     *
43
     * @param \Cake\Http\ServerRequest $request The request that contains login information.
44
     * @param \Cake\Http\Response $response Unused response object.
45
     * @return mixed False on login failure.  An array of User data on success.
46
     */
47
    public function authenticate(ServerRequest $request, Response $response)
48
    {
49
        return $this->getUser($request);
50
    }
51
52
    /**
53
     * Get a user based on information in the request. Primarily used by stateless authentication
54
     * systems like basic and digest auth.
55
     *
56
     * @param \Cake\Http\ServerRequest $request Request object.
57
     * @return mixed Either false or an array of user information
58
     */
59
    public function getUser(ServerRequest $request)
60
    {
61
        $authResult = $request->getAttribute('authentication')->getResult();
62
        if (!$authResult->isValid()) {
63
            return false;
64
        }
65
66
        $result = $request->getAttribute('identity');
67
68
        if ($result instanceof EntityInterface) {
69
            return $result->toArray();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result->toArray(); (array) is incompatible with the return type of the parent method CakeDC\Api\Service\Auth\...seAuthenticate::getUser of type boolean.

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...
70
        }
71
72
        return $result;
73
    }
74
}
75