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

HasSocials::getProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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