Passed
Push — master ( 6acc67...8caa54 )
by Bertrand
15:05 queued 07:08
created

SocialiteGatewayImpl::user()   B

Complexity

Conditions 8
Paths 54

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 20
c 0
b 0
f 0
nc 54
nop 1
dl 0
loc 35
rs 8.4444
1
<?php
2
3
4
namespace App\Src\UseCases\Infra\Gateway;
5
6
7
use App\Src\UseCases\Domain\Auth\SocialiteUser;
8
use App\Src\UseCases\Domain\Shared\Gateway\SocialiteGateway;
9
use Laravel\Socialite\Facades\Socialite;
10
11
class SocialiteGatewayImpl implements SocialiteGateway
12
{
13
    public function user(string $provider): SocialiteUser
14
    {
15
        if($provider === 'twitter'){
16
            $user = Socialite::driver($provider)->user();
17
        }elseif($provider === 'facebook'){
18
            $user = Socialite::driver($provider)->fields(['name', 'first_name', 'last_name', 'email'])->user();
0 ignored issues
show
Bug introduced by
The method fields() does not exist on Laravel\Socialite\Contracts\Provider. It seems like you code against a sub-type of Laravel\Socialite\Contracts\Provider such as Laravel\Socialite\Two\FacebookProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
            $user = Socialite::driver($provider)->/** @scrutinizer ignore-call */ fields(['name', 'first_name', 'last_name', 'email'])->user();
Loading history...
19
        }else {
20
            $user = Socialite::driver($provider)->stateless()->user();
0 ignored issues
show
Bug introduced by
The method stateless() does not exist on Laravel\Socialite\Contracts\Provider. It seems like you code against a sub-type of Laravel\Socialite\Contracts\Provider such as Laravel\Socialite\Two\AbstractProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
            $user = Socialite::driver($provider)->/** @scrutinizer ignore-call */ stateless()->user();
Loading history...
21
        }
22
23
        $email = $user->getEmail();
24
25
        // For twitter, we prefer to use the name. If empty we'll use the twitter account (nickname):
26
        $firstname = $user->getName() !== null ? $user->getName() :  $user->getNickname();
27
        if(!empty($user->user['given_name'])){
28
            // Google
29
            $firstname = $user->user['given_name'];
30
        }elseif (!empty($user->user['first_name'])){
31
            // Facebook
32
            $firstname = $user->user['first_name'];
33
        }
34
35
        $lastname = $user->getName();
36
        if(!empty($user->user['family_name'])){
37
            // Google
38
            $lastname = $user->user['family_name'];
39
        }elseif (!empty($user->user['last_name'])){
40
            // Facebook
41
            $lastname = $user->user['last_name'];
42
        }
43
44
        $id = $user->getId();
45
        $picture = $user->getAvatar();
46
47
        return new SocialiteUser($id, $email, $firstname, $lastname, $picture);
48
    }
49
}
50