LoginUserProvider::loadUserByOAuthUserResponse()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 28
ccs 0
cts 23
cp 0
rs 8.439
cc 6
eloc 20
nc 8
nop 1
crap 42
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: device
5
 * Date: 19.02.16
6
 * Time: 17:37
7
 */
8
9
namespace AppBundle\OAuthProvider;
10
11
12
use AppBundle\Entity\User;
13
use Doctrine\ORM\EntityManager;
14
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
15
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface;
16
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
17
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
18
use Symfony\Component\Security\Core\User\UserInterface;
19
use Symfony\Component\Security\Core\User\UserProviderInterface;
20
21
class LoginUserProvider implements UserProviderInterface, OAuthAwareUserProviderInterface
22
{
23
    /** @var  EntityManager $em */
24
    protected $em;
25
26 27
    public function __construct(EntityManager $em)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
27
    {
28 27
        $this->em = $em;
29 27
    }
30
31
    /**
32
     * Loads the user by a given UserResponseInterface object.
33
     *
34
     * @param UserResponseInterface $response
35
     *
36
     * @return UserInterface
37
     *
38
     * @throws UsernameNotFoundException if the user is not found
39
     */
40
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
41
    {
42
        $em = $this->em;
43
        $type = $response->getResourceOwner()->getName();
44
45
        $user = $em->getRepository('AppBundle:User')->findOneBy(['email' => $response->getEmail()]);
46
        if ($user === null) {
47
            $user = new User();
48
            $user->setEmail($response->getEmail())
49
                ->setFirstName($response->getFirstName() ?: $response->getUsername())
50
                ->setLastName($response->getLastName() ?: $response->getRealname())
51
                ->setIsActive(true);
52
            $em->persist($user);
53
            $em->flush();
54
55
        }
56
        if ($type === 'facebook') {
57
            $user->setFacebookToken($response->getAccessToken())
58
                ->setFacebookId($response->getUsername());
59
        }
60
        if ($type === 'google') {
61
            $user->setGoogleToken($response->getAccessToken())
62
                ->setGoogleId($response->getUsername());
63
        }
64
        $em->flush();
65
66
        return $user;
67
    }
68
69
    /**
70
     * Loads the user for the given username.
71
     *
72
     * This method must throw UsernameNotFoundException if the user is not
73
     * found.
74
     *
75
     * @param string $username The username
76
     *
77
     * @return UserInterface
78
     *
79
     * @see UsernameNotFoundException
80
     *
81
     * @throws UsernameNotFoundException if the user is not found
82
     */
83
    public function loadUserByUsername($username)
84
    {
85
        $user = $this->em->getRepository('AppBundle:User')->findOneBy(['email' => $username]);
86
        if (!$user) {
87
            throw new UsernameNotFoundException(sprintf("User '%s' not found.", $username));
88
        }
89
90
        return $user;
91
    }
92
93
    /**
94
     * Refreshes the user for the account interface.
95
     *
96
     * It is up to the implementation to decide if the user data should be
97
     * totally reloaded (e.g. from the database), or if the UserInterface
98
     * object can just be merged into some internal array of users / identity
99
     * map.
100
     *
101
     * @param UserInterface $user
102
     *
103
     * @return UserInterface
104
     *
105
     * @throws UnsupportedUserException if the account is not supported
106
     */
107
    public function refreshUser(UserInterface $user)
108
    {
109
        return $this->loadUserByUsername($user->getUsername());
110
    }
111
112
    /**
113
     * Whether this provider supports the given user class.
114
     *
115
     * @param string $class
116
     *
117
     * @return bool
118
     */
119
    public function supportsClass($class)
120
    {
121
        return $class instanceof User;
122
    }
123
124
    /**
125
     * Connects the response the the user object.
126
     *
127
     * @param UserInterface $user The user object
128
     * @param UserResponseInterface $response The oauth response
129
     */
130
    public function connect(UserInterface $user, UserResponseInterface $response)
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
    }
133
134
}