FOSUBUserProvider::loadUserByOAuthUserResponse()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 8.8981
c 0
b 0
f 0
cc 4
eloc 31
nc 6
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Badger\Bundle\UserBundle\Security;
4
5
use Badger\Component\Game\Repository\TagRepositoryInterface;
6
use FOS\UserBundle\Model\UserManagerInterface;
7
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
8
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
11
/**
12
 * User provider to handle oAuth responses.
13
 *
14
 * @author  Adrien Pétremann <[email protected]>
15
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
16
 *
17
 * Credits: https://gist.github.com/danvbe/4476697
18
 */
19
class FOSUBUserProvider extends BaseClass
20
{
21
    /** @var TagRepositoryInterface */
22
    private $tagRepository;
23
24
    /**
25
     * @param UserManagerInterface   $userManager
26
     * @param array                  $properties
27
     * @param TagRepositoryInterface $tagRepository
28
     */
29
    public function __construct(
30
        UserManagerInterface $userManager,
31
        array $properties,
32
        TagRepositoryInterface $tagRepository
33
    ) {
34
        parent::__construct($userManager, $properties);
35
36
        $this->tagRepository = $tagRepository;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function connect(UserInterface $user, UserResponseInterface $response)
43
    {
44
        $property = $this->getProperty($response);
45
        $username = $response->getUsername();
46
47
        // On connect - get the access token and the user ID
48
        $service = $response->getResourceOwner()->getName();
49
        $setter = 'set' . ucfirst($service);
50
        $setter_id = $setter . 'Id';
51
        $setter_token = $setter . 'AccessToken';
52
53
        // "Disconnect" previously connected users
54
        if (null !== $previousUser = $this->userManager->findUserBy([$property => $username])) {
55
            $previousUser->$setter_id(null);
56
            $previousUser->$setter_token(null);
57
            $this->userManager->updateUser($previousUser);
58
        }
59
60
        // Connect current user
61
        $user->$setter_id($username);
62
        $user->$setter_token($response->getAccessToken());
63
        $this->userManager->updateUser($user);
0 ignored issues
show
Compatibility introduced by
$user of type object<Symfony\Component...ore\User\UserInterface> is not a sub-type of object<FOS\UserBundle\Model\UserInterface>. It seems like you assume a child interface of the interface Symfony\Component\Security\Core\User\UserInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
70
    {
71
        $username = $response->getUsername();
72
        $user = $this->userManager->findUserBy([$this->getProperty($response) => $username]);
73
74
        // Email is mandatory, we fill it up with random thing first
75
        $email = uniqid('badger');
76
        if (null !== $response->getEmail()) {
77
            $email = $response->getEmail();
78
        }
79
80
        // When the user is registering
81
        if (null === $user) {
82
            $service = $response->getResourceOwner()->getName();
83
            $setter = 'set' . ucfirst($service);
84
            $setter_id = $setter . 'Id';
85
            $setter_token = $setter . 'AccessToken';
86
87
            // Create new user here
88
            $user = $this->userManager->createUser();
89
            $user->$setter_id($username);
90
            $user->$setter_token($response->getAccessToken());
91
92
            $user->setUsername($response->getNickname());
93
            $user->setEmail($email);
94
            $user->setPassword($username); // TODO: change
95
            $user->setProfilePicture($response->getProfilePicture());
96
            $user->setEnabled(true);
97
            $user->setDateRegistered(new \DateTime());
98
            $user->setNuts(0);
99
100
            $tag = $this->tagRepository->findOneBy(['isDefault' => true]);
101
            if (null !== $tag) {
102
                $user->addTag($tag);
103
            }
104
105
            $this->userManager->updateUser($user);
106
107
            return $user;
108
        }
109
110
        // If user exists - go with the HWIOAuth way
111
        $user = parent::loadUserByOAuthUserResponse($response);
112
        $serviceName = $response->getResourceOwner()->getName();
113
        $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
114
115
        // Update access token
116
        $user->$setter($response->getAccessToken());
117
118
        return $user;
119
    }
120
}
121