Password::init()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace Eole\Sandstone\OAuth2\Grant;
4
5
use League\OAuth2\Server\Grant\PasswordGrant;
6
use Symfony\Component\Security\Core\User\UserProviderInterface;
7
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
8
9
class Password extends PasswordGrant
10
{
11
    /**
12
     * @var UserProviderInterface
13
     */
14
    private $userProvider;
15
16
    /**
17
     * @var EncoderFactoryInterface
18
     */
19
    private $encoderFactory;
20
21
    /**
22
     * @param UserProviderInterface $userProvider
23
     * @param EncoderFactoryInterface $encoderFactory
24
     */
25
    public function __construct(UserProviderInterface $userProvider, EncoderFactoryInterface $encoderFactory)
26
    {
27
        $this->userProvider = $userProvider;
28
        $this->encoderFactory = $encoderFactory;
29
30
        $this->init();
31
    }
32
33
    /**
34
     * Init.
35
     */
36
    private function init()
37
    {
38
        $this->setVerifyCredentialsCallback(function ($username, $password) {
0 ignored issues
show
Bug introduced by
The method setVerifyCredentialsCallback() does not seem to exist on object<Eole\Sandstone\OAuth2\Grant\Password>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
            $user = $this->userProvider->loadUserByUsername($username);
40
41
            if (null === $user) {
42
                return false;
43
            }
44
45
            $encoder = $this->encoderFactory->getEncoder($user);
46
            $isPasswordValid = $encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt());
47
48
            if ($isPasswordValid) {
49
                return $username;
50
            } else {
51
                return false;
52
            }
53
        });
54
    }
55
}
56