Completed
Push — master ( c3c1f8...c31631 )
by Travis
02:47
created

HasSocials::addSocial()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 10
nc 4
nop 2
1
<?php
2
3
namespace NukaCode\Users\Traits;
4
5
use Laravel\Socialite\AbstractUser;
6
use NukaCode\Users\Models\User\Social;
7
8
trait HasSocials
9
{
10
    public function addSocial(AbstractUser $socialUser, $provider)
11
    {
12
        $refreshToken = isset($socialUser->refresh_token) && $socialUser->refresh_token
13
            ? $socialUser->refresh_token
14
            : null;
15
16
        $this->socials()->create([
17
            'provider'      => $provider,
18
            'social_id'     => $socialUser->getId(),
19
            'avatar'        => $socialUser->getAvatar(),
20
            'token'         => $socialUser->token,
21
            'refresh_token' => $refreshToken,
22
        ]);
23
    }
24
25
    public function getProvider($provider)
26
    {
27
        return $this->socials()->where('provider', $provider)->first();
28
    }
29
30
    public function hasProvider($provider)
31
    {
32
        return $this->socials()->where('provider', $provider)->count() > 0;
33
    }
34
35
    public function socials()
36
    {
37
        return $this->hasMany(Social::class, 'user_id');
38
    }
39
}
40