1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Svycka\SocialUser\Service; |
4
|
|
|
|
5
|
|
|
use Svycka\SocialUser\LocalUserProviderInterface; |
6
|
|
|
use Svycka\SocialUser\Storage\SocialUserStorageInterface; |
7
|
|
|
use Svycka\SocialUser\UserProfileInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Vytautas Stankus <[email protected]> |
11
|
|
|
* @license MIT |
12
|
|
|
*/ |
13
|
|
|
class SocialUserService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var LocalUserProviderInterface |
17
|
|
|
*/ |
18
|
|
|
private $localUserProvider; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var SocialUserStorageInterface |
22
|
|
|
*/ |
23
|
|
|
private $socialUserStorage; |
24
|
|
|
|
25
|
8 |
|
public function __construct( |
26
|
|
|
LocalUserProviderInterface $localUserProvider, |
27
|
|
|
SocialUserStorageInterface $socialUserStorage |
28
|
|
|
) { |
29
|
8 |
|
$this->localUserProvider = $localUserProvider; |
30
|
8 |
|
$this->socialUserStorage = $socialUserStorage; |
31
|
8 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Gets local user ID. |
35
|
|
|
* |
36
|
|
|
* First tries to find already existing social login, |
37
|
|
|
* then tries to merge to existing user by comparing email addresses |
38
|
|
|
* and finally will create new user if earlier scenarios failed. |
39
|
|
|
* |
40
|
|
|
* So in the end should return existing or newly created local user ID or if fails return null. |
41
|
|
|
* |
42
|
|
|
* @param string $provider Provider name. |
43
|
|
|
* @param UserProfileInterface $userProfile User identifier used with provider. |
44
|
|
|
* |
45
|
|
|
* @return int|null |
46
|
|
|
*/ |
47
|
7 |
|
public function getLocalUser($provider, UserProfileInterface $userProfile) |
48
|
|
|
{ |
49
|
7 |
|
if (empty($provider) || !is_string($provider)) { |
50
|
1 |
|
throw new \InvalidArgumentException('Invalid "provider" argument provided.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// if possible will use social login data to retrieve user ID |
54
|
6 |
|
$result = $this->socialUserStorage->findByProviderIdentifier($provider, $userProfile->getIdentifier()); |
55
|
6 |
|
if ($result) { |
56
|
1 |
|
return $result->getLocalUser(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// only reach below if we do not have this social user |
60
|
|
|
|
61
|
|
|
// We have to be sure here that email exists and is verified, otherwise we can't assume it's the same user. |
62
|
5 |
|
if (empty($userProfile->getEmail()) || !$userProfile->isEmailVerified()) { |
63
|
2 |
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Will try to find and merge to existing user by email address |
67
|
3 |
|
$user_id = $this->localUserProvider->findByEmail($userProfile->getEmail()); |
68
|
|
|
|
69
|
|
|
// if user with same email doesn't exist in the database then will create new local user |
70
|
3 |
|
if (!$user_id) { |
|
|
|
|
71
|
2 |
|
$user_id = $this->localUserProvider->createNewUser($userProfile); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// if same email address exists in database then add new social login for that user |
75
|
3 |
|
if ($user_id) { |
|
|
|
|
76
|
2 |
|
$this->socialUserStorage->addSocialUser($user_id, $userProfile->getIdentifier(), $provider); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
return $user_id; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: