SocialProviderManager::socialUserQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace MadWeb\SocialAuth;
4
5
use MadWeb\SocialAuth\Models\SocialProvider;
6
use Illuminate\Contracts\Auth\Authenticatable;
7
use MadWeb\SocialAuth\Events\SocialUserCreated;
8
use MadWeb\SocialAuth\Events\SocialUserAttached;
9
use Laravel\Socialite\Contracts\User as SocialUser;
10
use MadWeb\SocialAuth\Contracts\SocialAuthenticatable;
11
12
class SocialProviderManager
13
{
14
    /**
15
     * @var SocialProvider
16
     */
17
    protected $social;
18
19
    /**
20
     * SocialProviderManager constructor.
21
     * @param SocialProvider $social
22
     */
23 51
    public function __construct(SocialProvider $social)
24
    {
25 51
        $this->social = $social;
26 51
    }
27
28
    /**
29
     * @param string $key
30
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
31
     */
32 24
    public function socialUserQuery(string $key)
33
    {
34 24
        return $this->social->users()->wherePivot(config('social-auth.foreign_keys.socials'), $key);
35
    }
36
37
    /**
38
     * Gets user by unique social identifier.
39
     *
40
     * @param string $key
41
     * @return mixed
42
     */
43 18
    public function getUserByKey(string $key)
44
    {
45 18
        return $this->socialUserQuery($key)->first();
46
    }
47
48
    /**
49
     * @param SocialAuthenticatable $user
50
     * @param SocialUser $socialUser
51
     */
52 12
    public function attach(SocialAuthenticatable $user, SocialUser $socialUser)
53
    {
54 12
        $user->attachSocial(
55 12
            $this->social,
56 12
            $socialUser->getId(),
57 12
            $socialUser->token,
0 ignored issues
show
Bug introduced by
Accessing token on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
58 12
            $socialUser->expiresIn ?? null
0 ignored issues
show
Bug introduced by
Accessing expiresIn on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
59
        );
60
61 12
        event(new SocialUserAttached($user, $this->social, $socialUser));
62 12
    }
63
64
    /**
65
     * Create new system user by social user data.
66
     *
67
     * @param Authenticatable $userModel
68
     * @param SocialProvider $social
69
     * @param SocialUser $socialUser
70
     * @return Authenticatable
71
     */
72 6
    public function createNewUser(
73
        Authenticatable $userModel,
74
        SocialProvider $social,
75
        SocialUser $socialUser
76
    ): Authenticatable {
77 6
        $NewUser = $userModel->create(
0 ignored issues
show
Bug introduced by
The method create() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78 6
            $userModel->mapSocialData($socialUser)
0 ignored issues
show
Bug introduced by
The method mapSocialData() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
        );
80
81 6
        $NewUser->attachSocial(
82 6
            $social,
83 6
            $socialUser->getId(),
84 6
            $socialUser->token,
0 ignored issues
show
Bug introduced by
Accessing token on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
85 6
            $socialUser->expiresIn ?? null
0 ignored issues
show
Bug introduced by
Accessing expiresIn on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
86
        );
87
88 6
        event(new SocialUserCreated($NewUser));
89
90 6
        return $NewUser;
91
    }
92
}
93