|
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> |
|
|
|
|
|
|
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|null |
|
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
|
|
|
/** |
|
59
|
|
|
* @inheritDoc |
|
60
|
|
|
* @throws InvalidConfigException |
|
61
|
|
|
*/ |
|
62
|
|
|
public function findModelByPkOrIdentifier($pkOrIdentifier) |
|
63
|
|
|
{ |
|
64
|
|
|
try { |
|
65
|
|
|
$model = $this->findModelByPk($pkOrIdentifier); |
|
66
|
|
|
} catch (\Exception $e) { |
|
67
|
|
|
// Silently ignore. |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (empty($model)) { |
|
71
|
|
|
$model = $this->findModelByIdentifier($pkOrIdentifier); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $model; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|