|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services; |
|
4
|
|
|
|
|
5
|
|
|
use App\Repositories\UserRepository; |
|
6
|
|
|
use App\Socialite; |
|
7
|
|
|
use Auth; |
|
8
|
|
|
use Laravel\Socialite\Contracts\User as ProviderUser; |
|
9
|
|
|
use App\Repositories\SocialiteRepository; |
|
10
|
|
|
|
|
11
|
|
|
class SocialiteUser |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ProviderUser |
|
15
|
|
|
*/ |
|
16
|
|
|
private $user; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $provider; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* SocialiteUser constructor. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->socialRepository = $this->getSocialiteRepository(); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param $provider |
|
33
|
|
|
* @param $callback |
|
34
|
|
|
* @return $this |
|
35
|
|
|
*/ |
|
36
|
|
|
public function init($provider, $callback) |
|
37
|
|
|
{ |
|
38
|
|
|
$this |
|
39
|
|
|
->setProvider($provider) |
|
40
|
|
|
->setUser($callback); |
|
41
|
|
|
|
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Register. |
|
47
|
|
|
* |
|
48
|
|
|
* @return mixed|null |
|
49
|
|
|
*/ |
|
50
|
|
|
public function register() |
|
51
|
|
|
{ |
|
52
|
|
|
if ($this->socialRepository->checkProviderUser($this->getProvider(), $this->user()->getId())) |
|
53
|
|
|
{ |
|
54
|
|
|
$s_user = $this->socialRepository->getUserByProvider( |
|
55
|
|
|
$this->getProvider(), $this->user()->getId() |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
if($user = $s_user->user) |
|
59
|
|
|
return $user; |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$social = $this->getOrCreateEmptySocialUser(); |
|
65
|
|
|
|
|
66
|
|
|
if($this->checkEmail()) |
|
67
|
|
|
return $this->createSocialUserWithEmail($social); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return mixed |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getProvider() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->provider; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return ProviderUser |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getUser() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->user; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Set user. |
|
90
|
|
|
* |
|
91
|
|
|
* @param $user |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setUser($user) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->user = $user; |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Set provider. |
|
103
|
|
|
* |
|
104
|
|
|
* @param $provider |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setProvider($provider) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->provider = $provider; |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return ProviderUser |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function user() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->getUser(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get or create social user |
|
124
|
|
|
* |
|
125
|
|
|
* @return null|static |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function getOrCreateEmptySocialUser() |
|
128
|
|
|
{ |
|
129
|
|
|
if($this->socialRepository->checkProviderUser($this->getProvider(), $this->user()->getId())) |
|
130
|
|
|
return $this->socialRepository->getUserByProvider($this->getProvider(), $this->user()->getId()); |
|
131
|
|
|
|
|
132
|
|
|
return $this->socialRepository->createEmpty( |
|
133
|
|
|
$this->getProvider(), $this->user() |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return SocialiteRepository |
|
139
|
|
|
*/ |
|
140
|
|
|
private function getSocialiteRepository() |
|
141
|
|
|
{ |
|
142
|
|
|
return new SocialiteRepository(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return UserRepository |
|
147
|
|
|
*/ |
|
148
|
|
|
private function getUserRepository() |
|
149
|
|
|
{ |
|
150
|
|
|
return new UserRepository(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Associate user. |
|
155
|
|
|
* |
|
156
|
|
|
* @param $account |
|
157
|
|
|
* @param null $email |
|
158
|
|
|
* @return mixed |
|
159
|
|
|
*/ |
|
160
|
|
|
private function associateSocialiteUser($account, $email = null) |
|
161
|
|
|
{ |
|
162
|
|
|
if(! $email) |
|
163
|
|
|
$email = $this->user()->getEmail(); |
|
164
|
|
|
|
|
165
|
|
|
$user = $this->getUserRepository()->getByEmail($email); |
|
166
|
|
|
$account->user()->associate($user); |
|
167
|
|
|
$account->save(); |
|
168
|
|
|
|
|
169
|
|
|
return $user; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Check if email exists; |
|
174
|
|
|
* |
|
175
|
|
|
* @return bool |
|
176
|
|
|
*/ |
|
177
|
|
|
private function checkEmail() |
|
178
|
|
|
{ |
|
179
|
|
|
return (bool) $this->user()->getEmail(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param $social |
|
184
|
|
|
* @return bool |
|
185
|
|
|
*/ |
|
186
|
|
|
public function tryToAssociateUser($social, $email = null) |
|
187
|
|
|
{ |
|
188
|
|
|
if(! $email) |
|
189
|
|
|
$email = $this->user()->getEmail(); |
|
190
|
|
|
|
|
191
|
|
|
if($this->getUserRepository()->checkIfUserExists($email)) |
|
192
|
|
|
return $this->associateSocialiteUser($social, $email); |
|
193
|
|
|
|
|
194
|
|
|
return false; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Create social user. |
|
199
|
|
|
* |
|
200
|
|
|
* @param Socialite $social |
|
201
|
|
|
* @return mixed |
|
202
|
|
|
*/ |
|
203
|
|
|
private function createSocialUserWithEmail(Socialite $social) |
|
204
|
|
|
{ |
|
205
|
|
|
$this->tryToAssociateUser($social); |
|
206
|
|
|
$names = $this->getFirstAndLustNames(); |
|
207
|
|
|
|
|
208
|
|
|
return $this->getUserRepository()->create([ |
|
|
|
|
|
|
209
|
|
|
'email' => $this->user()->getEmail(), |
|
210
|
|
|
'name' => $this->user()->getName(), |
|
211
|
|
|
'password' => $this->users->hashPassword(str_random(45)), |
|
|
|
|
|
|
212
|
|
|
'firstname' => @$names[0], |
|
213
|
|
|
'lastname' => @$names[1] |
|
214
|
|
|
]); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param null $name |
|
219
|
|
|
* @return array |
|
220
|
|
|
*/ |
|
221
|
|
|
public function getFirstAndLustNames($name = null) |
|
222
|
|
|
{ |
|
223
|
|
|
if(! $name) |
|
224
|
|
|
$name = $this->user()->getName(); |
|
225
|
|
|
|
|
226
|
|
|
return explode(' ', $name); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Login the user. |
|
231
|
|
|
* |
|
232
|
|
|
* @param $user |
|
233
|
|
|
*/ |
|
234
|
|
|
public function login($user) |
|
235
|
|
|
{ |
|
236
|
|
|
\Auth::login($user, true); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Add avatar. |
|
241
|
|
|
* |
|
242
|
|
|
* @param $avatar |
|
243
|
|
|
* @return $this |
|
244
|
|
|
*/ |
|
245
|
|
|
public function avatar($avatar) |
|
246
|
|
|
{ |
|
247
|
|
|
(new ImageProcessor())->changeAvatar($avatar); |
|
248
|
|
|
|
|
249
|
|
|
return $this; |
|
250
|
|
|
} |
|
251
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: