Passed
Push — issue#767 ( 39899e...c3d4bb )
by Guilherme
10:59
created

UserProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 12.12%

Importance

Changes 0
Metric Value
dl 0
loc 113
ccs 4
cts 33
cp 0.1212
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsClass() 0 7 2
A createUser() 0 20 1
A __construct() 0 5 1
A refreshUser() 0 8 2
A loadUserByUsername() 0 12 2
1
<?php
2
/*
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Security\OIDC;
12
13
use Doctrine\ORM\EntityManager;
14
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
15
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
16
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
17
use Symfony\Component\Security\Core\User\UserProviderInterface;
18
use Symfony\Component\Security\Core\User\UserInterface;
19
use OAuth2\ServerBundle\User\OAuth2UserProvider;
20
21
class UserProvider extends OAuth2UserProvider implements UserProviderInterface
22
{
23
    private $em;
24
    private $encoderFactory;
25
26 14
    public function __construct(EntityManager $entityManager,
0 ignored issues
show
Coding Style introduced by
The first parameter of a multi-line function declaration must be on the line after the opening bracket
Loading history...
27
                                EncoderFactoryInterface $encoderFactory)
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 32
Loading history...
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
28
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
29 14
        $this->em             = $entityManager;
30 14
        $this->encoderFactory = $encoderFactory;
31 14
    }
32
33
    /**
34
     * Loads the user for the given username.
35
     *
36
     * This method must throw UsernameNotFoundException if the user is not
37
     * found.
38
     *
39
     * @param string $username The username
40
     *
41
     * @return UserInterface
42
     *
43
     * @see UsernameNotFoundException
44
     *
45
     * @throws UsernameNotFoundException if the user is not found
46
     *
47
     */
48
    public function loadUserByUsername($username)
49
    {
50
        $user = $this->em->getRepository('LoginCidadaoCoreBundle:Person')
51
            ->find($username);
52
53
        if (!$user) {
54
            throw new UsernameNotFoundException(sprintf(
55
                'Username "%s" not found.', $username
56
            ));
57
        }
58
59
        return $user;
60
    }
61
62
    /**
63
     * Refreshes the user for the account interface.
64
     *
65
     * It is up to the implementation to decide if the user data should be
66
     * totally reloaded (e.g. from the database), or if the UserInterface
67
     * object can just be merged into some internal array of users / identity
68
     * map.
69
     * @param UserInterface $user
70
     *
71
     * @return UserInterface
72
     *
73
     * @throws UnsupportedUserException if the account is not supported
74
     */
75
    public function refreshUser(UserInterface $user)
76
    {
77
        if (!$user instanceof OAuth2User) {
0 ignored issues
show
Bug introduced by
The type LoginCidadao\CoreBundle\Security\OIDC\OAuth2User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
78
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.',
79
                get_class($user)));
80
        }
81
82
        return $this->loadUserByUsername($user->getUsername());
83
    }
84
85
    /**
86
     * Whether this provider supports the given user class
87
     *
88
     * @param string $class
89
     *
90
     * @return Boolean
91
     */
92
    public function supportsClass($class)
93
    {
94
        if ($class == 'Person') {
95
            return true;
96
        }
97
98
        return false;
99
    }
100
101
    /**
102
     * Creates a new user
103
     *
104
     * @param string $username
105
     *
106
     * @param string $password
107
     *
108
     * @param array $roles
109
     *
110
     * @param array $scopes
111
     *
112
     * @return UserInterface
113
     */
114
    public function createUser($username, $password, array $roles = array(),
0 ignored issues
show
Coding Style introduced by
The first parameter of a multi-line function declaration must be on the line after the opening bracket
Loading history...
Coding Style introduced by
Multi-line function declarations must define one parameter per line
Loading history...
115
                                array $scopes = array())
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 32
Loading history...
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
116
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
117
        $user = parent::createUser();
0 ignored issues
show
Bug introduced by
The call to OAuth2\ServerBundle\User...rProvider::createUser() has too few arguments starting with username. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
        /** @scrutinizer ignore-call */ 
118
        $user = parent::createUser();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
118
119
        // Generate password
120
        $salt     = $this->generateSalt();
121
        $password = $this->encoderFactory->getEncoder($user)
122
            ->encodePassword($password, $salt);
123
124
        $user->setSalt($salt);
125
        $user->setPassword($password);
126
        $user->setUsername($username);
127
        $user->setRoles($roles);
128
        $user->setScopes($scopes);
129
130
        $this->em->persist($user);
131
        $this->em->flush();
132
133
        return $user;
134
    }
135
}
136