Passed
Push — master ( c21d64...956340 )
by Arthur
05:06
created

Auth0UserRepository::userEqualsProfile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 1
nc 4
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 4
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
use Foundation\Exceptions\Exception;
12
use Illuminate\Validation\UnauthorizedException;
13
use Modules\User\Contracts\UserServiceContract;
14
use Modules\User\Entities\User;
15
use MongoDB\BSON\ObjectId;
16
17
class Auth0UserRepository extends \Auth0\Login\Repository\Auth0UserRepository
18
{
19
    protected $service;
20
21
    /**
22
     * Auth0UserRepository constructor.
23
     *
24
     * @param $service
25
     */
26 3
    public function __construct(UserServiceContract $service)
27
    {
28 3
        $this->service = $service;
29 3
    }
30
31
    /* This class is used on api authN to fetch the user based on the jwt.*/
32 2
    public function getUserByDecodedJWT($jwt)
33
    {
34
        /*
35
         * The `sub` claim in the token represents the subject of the token
36
         * and it is always the `user_id`
37
         */
38 2
        $jwt->user_id = $jwt->sub;
39
40 2
        return $this->upsertUser($jwt);
41
    }
42
43
    public function getUserByUserInfo($userInfo)
44
    {
45
        return $this->upsertUser($userInfo['profile']);
46
    }
47
48 2
    protected function upsertUser($profile)
49
    {
50 2
        if (!isset($profile->user_id)) {
51
            throw new Exception('Missing token information: Auth0 user id is not set');
52
        }
53 2
        $identifier = explode('|', $profile->user_id);
54 2
        $identityProvider = $identifier[0];
55 2
        $id = $identifier[1];
56
57 2
        $user = $this->service->find($id);
58
59 2
        if ($user === null || !$this->userEqualsProfile($user, $profile)) {
60
            try {
61 1
                if ($user === null) {
62 1
                    $user = new User();
63
                }
64
65 1
                $user->_id = new ObjectId($id);
0 ignored issues
show
Bug Best Practice introduced by
The property _id does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
66 1
                $user->provider = $identityProvider;
0 ignored issues
show
Bug Best Practice introduced by
The property provider does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
67 1
                $user->email = $profile->email;
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...
68 1
                $user->username = $profile->nickname;
0 ignored issues
show
Bug Best Practice introduced by
The property username does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
69 1
                $user->name = $profile->name;
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...
70 1
                $user->avatar = $profile->picture;
0 ignored issues
show
Bug Best Practice introduced by
The property avatar does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
71 1
                $user->save();
72
            } catch (\Exception $exception) {
73
                throw new UnauthorizedException('Profile data is not set in the token ');
74
            }
75
        }
76
77 2
        return $user;
78
    }
79
80 1
    private function userEqualsProfile($user, $profile)
81
    {
82 1
        return $user->username === $profile->nickname && $user->email === $profile->email && $user->name === $profile->name && $user->avatar === $profile->picture;
83
    }
84
85
    public function getUserByIdentifier($identifier)
86
    {
87
    }
88
}
89