Passed
Pull Request — 1.2 (#405)
by
unknown
08:49
created

HandleOauth   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 62
ccs 0
cts 23
cp 0
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A oauthUpdateProvider() 0 12 1
A oauthCreateUser() 0 13 1
A oauthUser() 0 3 1
A oauthIsUserLinked() 0 7 1
1
<?php
2
3
namespace A17\Twill\Repositories\Behaviors;
4
5
trait HandleOauth
6
{
7
8
    /**
9
     * @param $oauthUser
10
     * @return A17\Twill\Models\User
0 ignored issues
show
Bug introduced by
The type A17\Twill\Repositories\B...s\A17\Twill\Models\User was not found. Did you mean A17\Twill\Models\User? If so, make sure to prefix the type with \.
Loading history...
11
     */
12
    public function oauthUser($oauthUser)
13
    {
14
        return $this->model->whereEmail($oauthUser->email)->first();
15
    }
16
17
    /**
18
     * @param $oauthUser
19
     * @param $provider
20
     * @return boolean
21
     */
22
    public function oauthIsUserLinked($oauthUser, $provider)
23
    {
24
        $user = $this->model->whereEmail($oauthUser->email)->first();
25
26
        return $user->providers()
27
            ->where(['provider' => $provider, 'oauth_id' => $oauthUser->id])
28
            ->exists();
29
    }
30
31
    /**
32
     * @param $oauthUser
33
     * @param $provider
34
     * @return A17\Twill\Models\User
35
     */
36
    public function oauthUpdateProvider($oauthUser, $provider)
37
    {
38
        $user = $this->model->whereEmail($oauthUser->email)->first();
39
        $provider = $user->providers()
40
            ->where(['provider' => $provider, 'oauth_id' => $oauthUser->id])
41
            ->first();
42
43
        $provider->token = $oauthUser->token;
44
        $provider->avatar = $oauthUser->avatar;
45
        $provider->save();
46
47
        return $user;
48
    }
49
50
    /**
51
     * @param $oauthUser
52
     * @return A17\Twill\Models\User
53
     */
54
    public function oauthCreateUser($oauthUser)
55
    {
56
57
        $user = $this->model->firstOrNew([
58
            'name' => $oauthUser->name,
59
            'email' => $oauthUser->email,
60
            'role' => config('twill.oauth.default_role'),
61
            'published' => true,
62
        ]);
63
64
        $user->save();
65
66
        return $user;
67
68
    }
69
70
}
71