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
|
|
|
return $authUser; |
32
|
|
|
} |
33
|
|
|
return $this->createSocialUser($socialUser); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Find social user. |
38
|
|
|
* |
39
|
|
|
* @param $socialUser |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
public function find($socialUser) |
43
|
|
|
{ |
44
|
|
|
return SocialUser::where('social_id', $socialUser->id) |
45
|
|
|
->where('social_type', $this->provider)->first()->user(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create social user. |
50
|
|
|
* |
51
|
|
|
* @param $socialUser |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function createSocialUser($socialUser) |
55
|
|
|
{ |
56
|
|
|
$user = $this->createUser($socialUser); |
57
|
|
|
SocialUser::create([ |
|
|
|
|
58
|
|
|
'user_id' => $user->id, |
59
|
|
|
'social_id' => $socialUser->id, |
60
|
|
|
'social_type' => $this->provider, |
61
|
|
|
'nickname' => $socialUser->nickname, |
62
|
|
|
'name' => $socialUser->name, |
63
|
|
|
'email' => $socialUser->email, |
64
|
|
|
'avatar' => $socialUser->avatar, |
65
|
|
|
]); |
66
|
|
|
return $user; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns field name to use at login. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
private function username() |
75
|
|
|
{ |
76
|
|
|
return config('auth.providers.users.field','email'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set provider |
81
|
|
|
* |
82
|
|
|
* @param $provider |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function provider($provider) |
86
|
|
|
{ |
87
|
|
|
$this->provider = $provider; |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create regular user. |
93
|
|
|
* |
94
|
|
|
* @param $socialUser |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
private function createUser($socialUser) |
98
|
|
|
{ |
99
|
|
|
$user = [ |
100
|
|
|
'name' => $socialUser->name, |
101
|
|
|
'email' => $socialUser->email, |
102
|
|
|
]; |
103
|
|
|
if ($this->username() === 'username') { |
104
|
|
|
$user['username'] = $socialUser->nickname; |
105
|
|
|
} |
106
|
|
|
$userClass = $this->userModel(); |
107
|
|
|
return $userClass::create($user); |
108
|
|
|
} |
109
|
|
|
} |
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.