Passed
Push — master ( caab97...76a5ed )
by Rutger
12:41
created

Oauth2UserRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 28
c 1
b 0
f 0
dl 0
loc 72
rs 10
ccs 30
cts 30
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserEntityByIdentifier() 0 16 3
A getModelClass() 0 3 1
A getUserEntityByUserCredentials() 0 36 6
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\components\repositories;
4
5
use League\OAuth2\Server\Entities\ClientEntityInterface;
6
use rhertogh\Yii2Oauth2Server\components\repositories\base\Oauth2BaseRepository;
7
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2UserRepositoryInterface;
8
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2PasswordGrantUserInterface;
9
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2UserInterface;
10
use yii\base\InvalidConfigException;
11
12
class Oauth2UserRepository extends Oauth2BaseRepository implements Oauth2UserRepositoryInterface
13
{
14
    /**
15
     * @inheritDoc
16
     * @return Oauth2UserInterface|string
17
     */
18 10
    public function getModelClass()
19
    {
20 10
        return $this->_module->identityClass;
21
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26 4
    public function getUserEntityByIdentifier($identifier)
27
    {
28 4
        $userClass = $this->getModelClass();
29 4
        $user = $userClass::findIdentity($identifier);
30
31 4
        if (empty($user)) {
32 1
            return null;
33
        }
34
35 3
        if (!($user instanceof Oauth2UserInterface)) {
36 1
            throw new \TypeError(
37 1
                $userClass . '::findIdentity() must return an instance of ' . Oauth2UserInterface::class
0 ignored issues
show
Bug introduced by
Are you sure $userClass of type rhertogh\Yii2Oauth2Serve...els\Oauth2UserInterface can be used in concatenation? Consider adding a __toString()-method. ( Ignorable by Annotation )

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

37
                /** @scrutinizer ignore-type */ $userClass . '::findIdentity() must return an instance of ' . Oauth2UserInterface::class
Loading history...
38
            );
39
        }
40
41 2
        return $user;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     * @throws InvalidConfigException
47
     */
48 6
    public function getUserEntityByUserCredentials(
49
        $username,
50
        $password,
51
        $grantType,
52
        ClientEntityInterface $clientEntity
53
    ) {
54 6
        $userClass = $this->getModelClass();
55 6
        if (!is_a($userClass, Oauth2PasswordGrantUserInterface::class, true)) {
56 1
            throw new \TypeError(
57 1
                'In order to support the `password` grant type, ' . $userClass
0 ignored issues
show
Bug introduced by
Are you sure $userClass of type rhertogh\Yii2Oauth2Serve...els\Oauth2UserInterface can be used in concatenation? Consider adding a __toString()-method. ( Ignorable by Annotation )

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

57
                'In order to support the `password` grant type, ' . /** @scrutinizer ignore-type */ $userClass
Loading history...
58 1
                    . ' must implement ' . Oauth2PasswordGrantUserInterface::class
59
            );
60
        }
61
62
        /** @var Oauth2UserInterface|null $user */
63 5
        $user = $userClass::findByUsername($username);
0 ignored issues
show
Bug introduced by
The method findByUsername() does not exist on rhertogh\Yii2Oauth2Serve...els\Oauth2UserInterface. It seems like you code against a sub-type of said class. However, the method does not exist in rhertogh\Yii2Oauth2Serve...Oauth2OidcUserInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

63
        /** @scrutinizer ignore-call */ 
64
        $user = $userClass::findByUsername($username);
Loading history...
64 5
        if (empty($user)) {
65 1
            return null;
66
        }
67 4
        if (!($user instanceof Oauth2UserInterface)) {
0 ignored issues
show
introduced by
$user is always a sub-type of rhertogh\Yii2Oauth2Serve...els\Oauth2UserInterface.
Loading history...
68 1
            throw new \TypeError(
69 1
                $userClass . '::findByUsername() must return an instance of ' . Oauth2UserInterface::class
70
            );
71
        }
72 3
        if (!($user instanceof Oauth2PasswordGrantUserInterface)) {
73 1
            throw new \TypeError(
74 1
                'In order to support the `password` grant type, ' . $userClass
75 1
                    . '::findByUsername() must return an instance of ' . Oauth2PasswordGrantUserInterface::class
76
            );
77
        }
78
79 2
        if ($user->validatePassword($password)) {
80 1
            return $user;
81
        }
82
83 1
        return null;
84
    }
85
}
86