Completed
Push — master ( 806356...cc6609 )
by Sergi Tur
02:02
created

EloquentSocialUserRepository::validName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Acacha\LaravelSocial\Repositories;
4
5
use Acacha\LaravelSocial\Models\SocialUser;
6
use Acacha\LaravelSocial\Traits\UserModel;
7
8
/**
9
 * Class EloquentSocialUserRepository.
10
 */
11
class EloquentSocialUserRepository implements SocialUserRepository
12
{
13
    use UserModel;
14
15
    /**
16
     * Social network.
17
     *
18
     * @var
19
     */
20
    protected $provider;
21
22
    /**
23
     * Return user if exists; create and return if doesn't.
24
     *
25
     * @param $socialUser
26
     * @return User
27
     */
28
    public function findOrCreateUser($socialUser)
29
    {
30
        if ($authUser = $this->find($socialUser)) {
31
            if ($user = $authUser->user) {
32
                return $user ;
33
            }
34
            return $this->createUser($socialUser);
35
        }
36
37
        $userClass = $this->userModel();
38
        if ($user = $userClass::where('email', $socialUser->email)->first()) {
39
            $this->createSocialUser($socialUser, $user->id);
40
            return $user;
41
        }
42
        $user = $this->createUser($socialUser);
43
        $this->createSocialUser($socialUser, $user->id);
44
        return $user;
45
    }
46
47
    /**
48
     * Find social user.
49
     *
50
     * @param $socialUser
51
     * @return mixed
52
     */
53
    public function find($socialUser)
54
    {
55
        return SocialUser::where('social_id', $socialUser->id)
56
               ->where('social_type', $this->provider)->first();
57
    }
58
59
    /**
60
     * Create social user.
61
     *
62
     * @param $socialUser
63
     * @param $userId
64
     * @return mixed
65
     */
66
    public function createSocialUser($socialUser, $userId)
67
    {
68
        return SocialUser::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on Acacha\LaravelSocial\Models\SocialUser. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
69
            'user_id'     => $userId,
70
            'social_id'   => $socialUser->id,
71
            'social_type' => $this->provider,
72
            'nickname'    => $socialUser->nickname,
73
            'name'        => $socialUser->name,
74
            'email'       => $socialUser->email,
75
            'avatar'      => $socialUser->avatar,
76
            'meta'        => json_encode($socialUser),
77
        ]);
78
    }
79
80
    /**
81
     * Returns field name to use at login.
82
     *
83
     * @return string
84
     */
85
    private function username()
86
    {
87
        return config('auth.providers.users.field', 'email');
88
    }
89
90
    /**
91
     * Set provider
92
     *
93
     * @param $provider
94
     * @return $this
95
     */
96
    public function provider($provider)
97
    {
98
        $this->provider = $provider;
99
        return $this;
100
    }
101
102
    /**
103
     * Create regular user.
104
     *
105
     * @param $socialUser
106
     * @return mixed
107
     */
108
    private function createUser($socialUser)
109
    {
110
        $user = [
111
            'name' => $this->validName($socialUser),
112
            'email' => $socialUser->email,
113
        ];
114
        if ($this->username() === 'username') {
115
            $user['username'] = $socialUser->nickname;
116
        }
117
        $userClass = $this->userModel();
118
        return $userClass::create($user);
119
    }
120
121
    /**
122
     * Provides always a valid (for database) name.
123
     *
124
     * @param $socialUser
125
     * @return mixed
126
     */
127
    private function validName($socialUser)
128
    {
129
        if ($socialUser->name) return $socialUser->name;
130
        //Github users could have no name use login instead
131
        if ($socialUser->login) return $socialUser->login;
132
        return 'Change your name';
133
    }
134
}
135