Psr7Authenticate   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
ccs 0
cts 13
cp 0
wmc 5
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 4 1
A getUser() 0 18 4
1
<?php
2
/**
3
 * Copyright 2016 - 2018, 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 - 2018, 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 Authentication\IdentityInterface;
29
use Cake\Datasource\EntityInterface;
30
use Cake\Http\Response;
31
use Cake\Http\ServerRequest;
32
33
/**
34
 * Class FormAuthenticate.
35
 */
36
class Psr7Authenticate extends BaseAuthenticate
37
{
38
39
    /**
40
     * Authenticates the identity contained in a request. Will use the `config.userModel`, and `config.fields`
41
     * to find POST data that is used to find a matching record in the `config.userModel`. Will return false if
42
     * there is no post data, either username or password is missing, or if the scope conditions have not been met.
43
     *
44
     * @param \Cake\Http\ServerRequest $request The request that contains login information.
45
     * @param \Cake\Http\Response $response Unused response object.
46
     * @return mixed False on login failure.  An array of User data on success.
47
     */
48
    public function authenticate(ServerRequest $request, Response $response)
49
    {
50
        return $this->getUser($request);
51
    }
52
53
    /**
54
     * Get a user based on information in the request. Primarily used by stateless authentication
55
     * systems like basic and digest auth.
56
     *
57
     * @param \Cake\Http\ServerRequest $request Request object.
58
     * @return mixed Either false or an array of user information
59
     */
60
    public function getUser(ServerRequest $request)
61
    {
62
        $authResult = $request->getAttribute('authentication')->getResult();
63
        if (!$authResult->isValid()) {
64
            return false;
65
        }
66
67
        $result = $request->getAttribute('identity');
68
69
        if ($result instanceof IdentityInterface) {
0 ignored issues
show
Bug introduced by
The class Authentication\IdentityInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
70
            $result = $result->getOriginalData();
71
        }
72
        if ($result instanceof EntityInterface) {
73
            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...
74
        }
75
76
        return $result;
77
    }
78
}
79