Passed
Push — master ( e5b31a...cf340d )
by Arthur
04:48
created

Auth0UserProfileStorageDriver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 104
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A boot() 0 6 2
A profileHasChanged() 0 6 1
A loadServices() 0 17 4
A run() 0 11 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 13.10.18
6
 * Time: 19:08
7
 */
8
9
namespace Modules\Auth0\Drivers;
10
11
12
use Foundation\Exceptions\NotImplementedException;
13
use Illuminate\Validation\UnauthorizedException;
14
use Modules\Auth0\Abstracts\Auth0ChangeDetectorGuard;
0 ignored issues
show
Bug introduced by
The type Modules\Auth0\Abstracts\Auth0ChangeDetectorGuard 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...
15
use Modules\Auth0\Abstracts\Auth0IdentityProviderTransformer;
16
use Modules\Auth0\Abstracts\Auth0ProfileValidator;
17
use Modules\Auth0\Guards\Auth0DatabaseProfileChangeDetector;
0 ignored issues
show
Bug introduced by
The type Modules\Auth0\Guards\Aut...seProfileChangeDetector 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...
18
use Modules\Auth0\Guards\Auth0FacebookProfileChangeDetector;
0 ignored issues
show
Bug introduced by
The type Modules\Auth0\Guards\Aut...okProfileChangeDetector 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...
19
use Modules\Auth0\Guards\Auth0GoogleProfileChangeDetector;
0 ignored issues
show
Bug introduced by
The type Modules\Auth0\Guards\Aut...leProfileChangeDetector 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...
20
use Modules\Auth0\Transformers\Auth0DatabaseProfileTransformer;
21
use Modules\Auth0\Transformers\Auth0FacebookProfileTransformer;
22
use Modules\Auth0\Transformers\Auth0GoogleProfileTransformer;
23
use Modules\Auth0\Validators\Auth0DatabaseProfileValidator;
24
use Modules\Auth0\Validators\Auth0FacebookProfileValidator;
25
use Modules\Auth0\Validators\Auth0GoogleProfileValidator;
26
use Modules\User\Entities\User;
27
28
class Auth0UserProfileStorageDriver
29
{
30
    /**
31
     * @var \stdClass
32
     */
33
    protected $profile;
34
    /**
35
     * @var User
36
     */
37
    protected $user;
38
    /**
39
     * @var string
40
     */
41
    protected $identityProvider;
42
43
    /**
44
     * @var array
45
     */
46
    protected $requiredProfileAttributes = [
47
        "email",
48
        "name",
49
        "avatar"
50
    ];
51
52
    /**
53
     * @var Auth0IdentityProviderTransformer
54
     */
55
    protected $transformer;
56
57
    /**
58
     * @var Auth0ProfileValidator
59
     */
60
    protected $validator;
61
62
    /**
63
     * Auth0UserStorageDriver constructor.
64
     * @param $profile
65
     * @param $user
66
     * @param $identityProvider
67
     */
68
    public function __construct(User $user, \stdClass $profile, string $identityProvider)
69
    {
70
        $this->profile = $profile;
71
        $this->user = $user;
72
        $this->identityProvider = $identityProvider;
73
        $this->boot();
74
    }
75
76
77
    /**
78
     * @throws NotImplementedException | UnauthorizedException
79
     */
80
    protected function boot()
81
    {
82
        $this->loadServices();
83
84
        if (!$this->validator->validate($this->profile)) {
85
            throw new UnauthorizedException('Invalid profile data found in the access token');
86
        }
87
    }
88
89
    /**
90
     * @return void
91
     * @throws NotImplementedException
92
     */
93
    protected function loadServices()
94
    {
95
        switch ($this->identityProvider) {
96
            case "auth0":
97
                $this->transformer = new Auth0DatabaseProfileTransformer();
98
                $this->validator = new Auth0DatabaseProfileValidator();
99
                break;
100
            case "facebook":
101
                $this->transformer = new Auth0FacebookProfileTransformer();
102
                $this->validator = new Auth0FacebookProfileValidator();
103
                break;
104
            case "google-oauth2":
105
                $this->transformer = new Auth0GoogleProfileTransformer();
106
                $this->validator = new Auth0GoogleProfileValidator();
107
                break;
108
            default:
109
                throw new NotImplementedException('Unsupported identity provider');
110
        }
111
    }
112
113
    public function run()
114
    {
115
        $profile = $this->transformer->transformProfile($this->profile);
116
117
        if ($this->profileHasChanged()) {
118
            $profile['provider'] = $this->identityProvider;
119
            $this->user->fill($profile);
120
            $this->user->save();
121
        }
122
123
        return $this->user;
124
    }
125
126
    protected function profileHasChanged(): bool
127
    {
128
        $user = $this->user->toArray();
129
        $profile = (array)$this->profile;
130
131
        return !array_is_subset_of($profile, $user);
132
    }
133
}
134