Passed
Push — master ( a5dfb5...866892 )
by Gabriel
04:31
created

ModelsHelper::setRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace ByTIC\Hello\Utility;
4
5
use ByTIC\Hello\Models\AccessTokens\Tokens;
6
use ByTIC\Hello\Models\AuthCodes\AuthCodes;
7
use ByTIC\Hello\Models\Clients\Clients;
8
use ByTIC\Hello\Models\RefreshTokens\RefreshTokens;
9
use ByTIC\Hello\Models\Scopes\Scopes;
10
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
11
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
12
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
13
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
14
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
15
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
16
use Nip\Records\Locator\ModelLocator;
17
use Nip\Records\RecordManager;
18
19
/**
20
 * Class ModelsHelper
21
 * @package ByTIC\Hello\Utility
22
 */
23
class ModelsHelper
24
{
25
26
    protected static $resolved = [];
27
28
    protected static $repositories = [
29
        AccessTokenRepositoryInterface::class => Tokens::class,
30
        ClientRepositoryInterface::class => Clients::class,
31
        RefreshTokenRepositoryInterface::class => RefreshTokens::class,
32
        ScopeRepositoryInterface::class => Scopes::class,
33
        AuthCodeRepositoryInterface::class => AuthCodes::class,
34
        UserRepositoryInterface::class => null,
35
    ];
36
37 6
    /**
38
     * @return Clients|\Nip\Records\AbstractModels\RecordManager
39 6
     */
40
    public static function clients()
41
    {
42
        return static::getRepository(ClientRepositoryInterface::class);
43
    }
44
45 6
    /**
46
     * @param RecordManager $manager
47 6
     */
48 6
    public static function useClientsManager(RecordManager $manager)
49
    {
50
        static::setRepository(ClientRepositoryInterface::class, $manager);
51
    }
52
53
    /**
54
     * @return Tokens|\Nip\Records\AbstractModels\RecordManager
55
     */
56
    public static function accessTokens()
57
    {
58
        return static::getRepository(AccessTokenRepositoryInterface::class);
59
    }
60
61
    /**
62
     * @return Scopes|\Nip\Records\AbstractModels\RecordManager
63
     */
64
    public static function scopes()
65
    {
66
        return static::getRepository(ScopeRepositoryInterface::class);
67
    }
68
69
    /**
70 6
     * @return array
71
     */
72 6
    public static function repositories()
73 6
    {
74
        return static::$repositories;
75
    }
76
77
    /**
78
     * @return array
79 1
     */
80
    public static function reset()
81 1
    {
82
        return static::$resolved =[];
83
    }
84
85
    /**
86
     * @param $alias
87
     * @return \Nip\Records\AbstractModels\RecordManager
88
     */
89
    protected static function getRepository($alias)
90
    {
91
        if (!isset(static::$resolved[$alias])) {
92
            static::$resolved[$alias] = static::generateRepository($alias);
93
        }
94
        return static::$resolved[$alias];
95
    }
96
97
    /**
98
     * @param $interface
99
     * @return \Nip\Records\AbstractModels\RecordManager
100
     */
101
    protected static function generateRepository($interface)
102
    {
103
        $class = ConfigHelper::get('repositories.'.$interface, false);
104
        if (!empty($class) && class_exists($class)) {
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type Nip\Config\Config; however, parameter $class_name of class_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

104
        if (!empty($class) && class_exists(/** @scrutinizer ignore-type */ $class)) {
Loading history...
105
            return ModelLocator::get($class);
106
        }
107
        return ModelLocator::get(static::$repositories[$interface]);
108
    }
109
110
    /**
111
     * @param $interface
112
     * @param RecordManager $manager
113
     */
114
    protected static function setRepository($interface, $manager)
115
    {
116
        static::$resolved[$interface] = $manager;
117
        ModelLocator::set($interface, $manager);
118
    }
119
}
120