Passed
Pull Request — master (#182)
by Arman
06:15 queued 03:14
created

AuthManager::getAuthService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Auth;
16
17
use Quantum\Libraries\Mailer\MailerException;
18
use Quantum\Exceptions\ServiceException;
19
use Quantum\Exceptions\ConfigException;
20
use Quantum\Libraries\JWToken\JWToken;
21
use Quantum\Exceptions\LangException;
22
use Quantum\Libraries\Hasher\Hasher;
23
use Quantum\Exceptions\DiException;
24
use Quantum\Factory\ServiceFactory;
25
use Quantum\Loader\Setup;
26
use ReflectionException;
27
28
/**
29
 * Class AuthManager
30
 * @package Quantum\Libraries\Auth
31
 */
32
class AuthManager
33
{
34
35
    const ADAPTERS = [
36
        'web',
37
        'api',
38
    ];
39
40
    /**
41
     * @var AuthenticatableInterface
42
     */
43
    private static $adapter;
44
45
    /**
46
     * Get Handler
47
     * @return AuthenticatableInterface
48
     * @throws AuthException
49
     * @throws ConfigException
50
     * @throws DiException
51
     * @throws ReflectionException
52
     * @throws LangException
53
     * @throws ServiceException
54
     * @throws MailerException
55
     */
56
    public static function getHandler(): AuthenticatableInterface
57
    {
58
        if (self::$adapter !== null) {
59
            return self::$adapter;
60
        }
61
62
        self::loadConfigs();
63
64
        $authAdapter = config()->get('auth.type');
65
66
        if (!in_array($authAdapter, self::ADAPTERS)) {
67
            throw AuthException::undefinedAuthType($authAdapter);
0 ignored issues
show
Bug introduced by
It seems like $authAdapter can also be of type null; however, parameter $name of Quantum\Libraries\Auth\A...on::undefinedAuthType() 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

67
            throw AuthException::undefinedAuthType(/** @scrutinizer ignore-type */ $authAdapter);
Loading history...
68
        }
69
70
        $authService = ServiceFactory::create(config()->get('auth.service'));
0 ignored issues
show
Bug introduced by
It seems like config()->get('auth.service') can also be of type null; however, parameter $serviceClass of Quantum\Factory\ServiceFactory::create() 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

70
        $authService = ServiceFactory::create(/** @scrutinizer ignore-type */ config()->get('auth.service'));
Loading history...
71
72
        if (!($authService instanceof AuthServiceInterface)) {
73
            throw AuthException::incorrectAuthService();
74
        }
75
76
        $mailer = mailer();
77
        $hasher = new Hasher();
78
        $jwt = null;
79
80
        if ($authAdapter == 'api') {
81
            $jwt = (new JWToken())
82
                ->setLeeway(1)
83
                ->setClaims((array)config()->get('auth.claims'));
84
        }
85
86
        $authAdapterClassName = __NAMESPACE__ . '\\Adapters\\' . ucfirst($authAdapter) . 'Adapter';
0 ignored issues
show
Bug introduced by
It seems like $authAdapter can also be of type null; however, parameter $string of ucfirst() 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

86
        $authAdapterClassName = __NAMESPACE__ . '\\Adapters\\' . ucfirst(/** @scrutinizer ignore-type */ $authAdapter) . 'Adapter';
Loading history...
87
88
        return self::$adapter = $authAdapterClassName::getInstance($authService, $mailer, $hasher, $jwt);
89
    }
90
91
    /**
92
     * @throws AuthException
93
     * @throws ConfigException
94
     * @throws DiException
95
     * @throws LangException
96
     * @throws ReflectionException
97
     */
98
    private static function loadConfigs()
99
    {
100
        if (!config()->has('auth')) {
101
            config()->import(new Setup('Config', 'auth'));
102
        }
103
104
        if (!config()->has('auth.type')) {
105
            throw AuthException::misconfiguredAuthConfig();
106
        }
107
108
        if (!config()->has('mailer') || !config()->has('mailer.current')) {
109
            config()->import(new Setup('config', 'mailer'));
110
        }
111
    }
112
}
113