Passed
Push — master ( f52d5c...0b49e9 )
by Rutger
03:13
created

Oauth2ModelRepositoryTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 6
eloc 17
c 0
b 0
f 0
dl 0
loc 46
ccs 10
cts 20
cp 0.5
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findModelByIdentifier() 0 15 3
A findModelByPk() 0 15 3
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\components\repositories\traits;
4
5
use rhertogh\Yii2Oauth2Server\helpers\DiHelper;
6
use rhertogh\Yii2Oauth2Server\interfaces\models\base\Oauth2ActiveRecordInterface;
7
use rhertogh\Yii2Oauth2Server\interfaces\models\base\Oauth2IdentifierInterface;
8
use yii\base\InvalidConfigException;
9
10
trait Oauth2ModelRepositoryTrait
11
{
12
    /**
13
     * @inheritDoc
14
     * @return class-string<Oauth2ActiveRecordInterface>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Oauth2ActiveRecordInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Oauth2ActiveRecordInterface>.
Loading history...
15
     */
16
    abstract public function getModelClass();
17
18
    public function findModelByPk($pk)
19
    {
20
        $class = $this->getModelClass();
21
        /** @var class-string<Oauth2ActiveRecordInterface> $className */
22
        $className = DiHelper::getValidatedClassName($class);
23
24
        $result = $className::findByPk($pk);
25
        if ($result !== null && !($result instanceof $class)) {
26
            throw new InvalidConfigException(
27
                $className . '::findByPk() returns '
28
                . get_class($result) . ' which must implement ' . $class
29
            );
30
        }
31
32
        return $result;
33
    }
34
35
    /**
36
     * @inheritDoc
37
     * @param string $identifier
38
     * @return Oauth2IdentifierInterface
39
     * @throws InvalidConfigException
40
     */
41 22
    public function findModelByIdentifier($identifier)
42
    {
43 22
        $class = $this->getModelClass();
44
        /** @var class-string<Oauth2IdentifierInterface> $className */
45 22
        $className = DiHelper::getValidatedClassName($class);
46
47 21
        $result = $className::findByIdentifier($identifier);
48 21
        if ($result !== null && !($result instanceof $class)) {
49 1
            throw new InvalidConfigException(
50 1
                $className . '::findByIdentifier() returns '
51 1
                    . get_class($result) . ' which must implement ' . $class
52 1
            );
53
        }
54
55 20
        return $result;
56
    }
57
}
58