Passed
Push — master ( 660c80...17f60a )
by Arthur
08:30
created

Auth0UserRepository::getUserByIdentifier()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 04.10.18
6
 * Time: 17:25
7
 */
8
9
namespace Foundation\Repositories;
10
11
12
use Modules\User\Contracts\UserServiceContract;
13
use Modules\User\Entities\User;
14
15
class Auth0UserRepository extends \Auth0\Login\Repository\Auth0UserRepository
16
{
17
    protected $service;
18
19
    /**
20
     * Auth0UserRepository constructor.
21
     * @param $service
22
     */
23
    public function __construct(UserServiceContract $service)
24
    {
25
        $this->service = $service;
26
    }
27
28
29
    /* This class is used on api authN to fetch the user based on the jwt.*/
30
    public function getUserByDecodedJWT($jwt) {
31
        /*
32
         * The `sub` claim in the token represents the subject of the token
33
         * and it is always the `user_id`
34
         */
35
        $jwt->user_id = $jwt->sub;
36
37
        return $this->upsertUser($jwt);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->upsertUser($jwt) returns the type Modules\User\Entities\User which is incompatible with the return type mandated by Auth0\Login\Contract\Aut...::getUserByDecodedJWT() of Illuminate\Contracts\Auth\Authenticatable.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
38
    }
39
40
    public function getUserByUserInfo($userInfo) {
41
        return $this->upsertUser($userInfo['profile']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->upsertUser($userInfo['profile']) returns the type Modules\User\Entities\User which is incompatible with the return type mandated by Auth0\Login\Contract\Aut...ry::getUserByUserInfo() of Illuminate\Contracts\Auth\Authenticatable.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
42
    }
43
44
    protected function upsertUser($profile) {
45
46
        // Note: Requires configured database access
47
        $user = $this->service->getUserByAuth0Id($profile->user_id);
48
49
        if ($user === null) {
50
            // If not, create one
51
            $user = new User();
52
            $user->email = $profile->email; // you should ask for the email scope
0 ignored issues
show
Bug Best Practice introduced by
The property email does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
53
            $user->auth0_id = $profile->user_id;
0 ignored issues
show
Bug Best Practice introduced by
The property auth0_id does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
54
            $user->name = $profile->name; // you should ask for the name scope
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
55
            $user->save();
56
        }
57
58
        return $user;
59
    }
60
61
    public function getUserByIdentifier($identifier) {
62
        //Get the user info of the user logged in (probably in session)
63
        $user = \App::make('auth0')->getUser();
64
65
        if ($user === null) return null;
0 ignored issues
show
introduced by
The condition $user === null is always false.
Loading history...
66
67
        // build the user
68
        $user = $this->getUserByUserInfo($user);
69
70
        // it is not the same user as logged in, it is not valid
71
        if ($user && $user->auth0id == $identifier) {
0 ignored issues
show
Bug Best Practice introduced by
The property auth0id does not exist on Modules\User\Entities\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
72
            return $user;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user returns the type Modules\User\Entities\User which is incompatible with the return type mandated by Auth0\Login\Contract\Aut...::getUserByIdentifier() of Illuminate\Contracts\Auth\Authenticatable.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
73
        }
74
    }
75
}
76