SocialUserService   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 69
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getLocalUser() 0 34 8
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user_id of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user_id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
76 2
            $this->socialUserStorage->addSocialUser($user_id, $userProfile->getIdentifier(), $provider);
77
        }
78
79 3
        return $user_id;
80
    }
81
}
82