UserProvider::createUserByOAuthUserResponse()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 37
rs 8.439
cc 6
eloc 19
nc 5
nop 1
1
<?php
2
3
namespace DoS\UserBundle\OAuth;
4
5
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
6
use DoS\UserBundle\Model\UserOAuthInterface;
7
use DoS\UserBundle\Model\UserInterface as DoSUserInterface;
8
use Symfony\Component\Security\Core\User\UserInterface;
9
use Sylius\Bundle\UserBundle\OAuth\UserProvider as SyliusUserProvider;
10
use Sylius\Component\User\Model\CustomerInterface;
11
12
/**
13
 * Loading and ad-hoc creation of a user by an OAuth sign-in provider account.
14
 */
15
class UserProvider extends SyliusUserProvider
16
{
17
    /**
18
     * @param UserResponseInterface|ResourceResponse $response
19
     *
20
     * @return UserInterface
21
     */
22
    protected function createUserByOAuthUserResponse(UserResponseInterface $response)
23
    {
24
        /** @var DoSUserInterface $user */
25
        $user = $this->userFactory->createNew();
26
        /** @var CustomerInterface $customer */
27
        $customer = $this->customerFactory->createNew();
28
29
        // set default values taken from OAuth sign-in provider account
30
        // todo: check security configuration provide by `fos....username_email`
31
        if (null === $response->getEmail()) {
32
            throw new AccountNoEmailException();
33
        }
34
35
        // set default values taken from OAuth sign-in provider account
36
        if (null !== $email = $response->getEmail()) {
37
            $customer->setEmail($email);
38
        }
39
40
        if (!$user->getUsername()) {
41
            $user->setUsername($response->getEmail() ?: $response->getNickname());
42
        }
43
44
        // set random password to prevent issue with not nullable field & potential security hole
45
        $user->setPlainPassword(substr(sha1($response->getAccessToken()), 0, 10));
46
47
        $user->setDisplayName($response->getNickname());
48
        $user->setLocale($response->getLocale());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface HWI\Bundle\OAuthBundle\O...e\UserResponseInterface as the method getLocale() does only exist in the following implementations of said interface: DoS\UserBundle\OAuth\FacebookResponse, DoS\UserBundle\OAuth\GithubResponse, DoS\UserBundle\OAuth\GoogleResponse, DoS\UserBundle\OAuth\ResourceResponse.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
49
        $user->setCustomer($customer);
50
        $user->confirmed();
51
52
        $customer->setFirstName($response->getFirstName());
53
        $customer->setLastName($response->getLastName());
54
        $customer->setGender($response->getGender() ?: CustomerInterface::UNKNOWN_GENDER);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface HWI\Bundle\OAuthBundle\O...e\UserResponseInterface as the method getGender() does only exist in the following implementations of said interface: DoS\UserBundle\OAuth\FacebookResponse, DoS\UserBundle\OAuth\GithubResponse, DoS\UserBundle\OAuth\GoogleResponse, DoS\UserBundle\OAuth\ResourceResponse.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
55
        $customer->setBirthday($response->getBirthday());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface HWI\Bundle\OAuthBundle\O...e\UserResponseInterface as the method getBirthday() does only exist in the following implementations of said interface: DoS\UserBundle\OAuth\FacebookResponse, DoS\UserBundle\OAuth\GithubResponse, DoS\UserBundle\OAuth\GoogleResponse, DoS\UserBundle\OAuth\ResourceResponse.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
56
57
        return $this->updateUserByOAuthUserResponse($user, $response);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function updateUserByOAuthUserResponse(UserInterface $user, UserResponseInterface $response)
64
    {
65
        /** @var UserOAuthInterface $oauth */
66
        $oauth = $this->oauthFactory->createNew();
67
        $oauth->setIdentifier($response->getUsername());
68
        $oauth->setProvider($response->getResourceOwner()->getName());
69
        $oauth->setAccessToken($response->getAccessToken());
70
        $oauth->setProfilePicture($response->getProfilePicture());
71
72
        /* @var DoSUserInterface $user */
73
        $user->addOAuthAccount($oauth);
74
75
        $this->userManager->persist($user);
76
        $this->userManager->flush();
77
78
        return $user;
79
    }
80
}
81