Passed
Push — master ( 848980...7bc374 )
by Arthur
04:30
created

Auth0Service   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 56
ccs 22
cts 25
cp 0.88
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserByUserInfo() 0 3 1
A __construct() 0 3 1
A upsertUser() 0 25 4
A getUserByDecodedJWT() 0 9 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 13.10.18
6
 * Time: 20:50
7
 */
8
9
namespace Modules\Auth0\Services;
10
11
12
use Auth0\Login\Repository\Auth0UserRepository;
0 ignored issues
show
Bug introduced by
The type Auth0\Login\Repository\Auth0UserRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Modules\Auth0\Drivers\Auth0UserProfileStorageDriver;
14
use Modules\User\Contracts\UserServiceContract;
15
use Modules\User\Events\UserRegisteredEvent;
16
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
17
18
class Auth0Service extends Auth0UserRepository
19
{
20
    protected $service;
21
22
    /**
23
     * Auth0UserRepository constructor.
24
     *
25
     * @param $service
26
     */
27 14
    public function __construct(UserServiceContract $service)
28
    {
29 14
        $this->service = $service;
30 14
    }
31
32
    /* This class is used on api authN to fetch the user based on the jwt.*/
33 9
    public function getUserByDecodedJWT($jwt)
34
    {
35
        /*
36
         * The `sub` claim in the token represents the subject of the token
37
         * and it is always the `user_id`
38
         */
39 9
        $jwt->user_id = $jwt->sub;
40
41 9
        return $this->upsertUser($jwt);
42
    }
43
44
    public function getUserByUserInfo($userInfo)
45
    {
46
        return $this->upsertUser($userInfo['profile']);
47
    }
48
49 9
    protected function upsertUser($profile)
50
    {
51 9
        if (!isset($profile->user_id)) {
52
            throw new BadRequestHttpException('Missing token information: Auth0 user id is not set');
53
        }
54 9
        $identifier = explode('|', $profile->user_id);
55 9
        $identityProvider = $identifier[0];
56 9
        $id = $identifier[1];
57
58 9
        $user = $this->service->find($id);
59 9
        $newUser = false;
60 9
        if ($user === null) {
61 9
            $user = $this->service->newUser([
62 9
                "identity_id" => $id
63
            ]);
64 9
            $newUser = true;
65
        }
66 9
        $driver = new Auth0UserProfileStorageDriver($user, $profile, $identityProvider);
67 9
        $user = $driver->run();
68
69 9
        if ($newUser) {
70 9
            event(new UserRegisteredEvent($user));
71
        }
72
73 9
        return $user;
74
    }
75
}
76