Passed
Pull Request — master (#75)
by Arman
05:05 queued 02:36
created

AuthManager::loadConfigs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 4
nop 0
dl 0
loc 8
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.8.0
13
 */
14
15
namespace Quantum\Libraries\Auth;
16
17
use Quantum\Libraries\JWToken\JWToken;
18
use Quantum\Exceptions\AuthException;
19
use Quantum\Libraries\Mailer\Mailer;
20
use Quantum\Libraries\Hasher\Hasher;
21
use Quantum\Factory\ServiceFactory;
22
use Quantum\Mvc\QtService;
23
use Quantum\Loader\Setup;
24
25
/**
26
 * Class AuthManager
27
 * @package Quantum\Libraries\Auth
28
 */
29
class AuthManager
30
{
31
32
    /**
33
     * Get Handler
34
     * @return \Quantum\Libraries\Auth\ApiAuth|\Quantum\Libraries\Auth\WebAuth
35
     * @throws \Quantum\Exceptions\AuthException
36
     * @throws \Quantum\Exceptions\ConfigException
37
     * @throws \Quantum\Exceptions\DiException
38
     * @throws \ReflectionException
39
     */
40
    public static function getHandler()
41
    {
42
        self::loadConfigs();
43
            
44
        switch (config()->has('auth.type')) {
45
            case 'web':
46
                return WebAuth::getInstance(self::getAuthService(), new Mailer, new Hasher);
0 ignored issues
show
Bug introduced by
self::getAuthService() of type Quantum\Mvc\QtService is incompatible with the type Quantum\Libraries\Auth\AuthServiceInterface expected by parameter $authService of Quantum\Libraries\Auth\WebAuth::getInstance(). ( Ignorable by Annotation )

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

46
                return WebAuth::getInstance(/** @scrutinizer ignore-type */ self::getAuthService(), new Mailer, new Hasher);
Loading history...
47
            case 'api':
48
                $jwt = (new JWToken())->setLeeway(1)->setClaims((array) config()->get('auth.claims'));
49
                return ApiAuth::getInstance(self::getAuthService(), new Mailer, new Hasher, $jwt);
0 ignored issues
show
Bug introduced by
self::getAuthService() of type Quantum\Mvc\QtService is incompatible with the type Quantum\Libraries\Auth\AuthServiceInterface expected by parameter $authService of Quantum\Libraries\Auth\ApiAuth::getInstance(). ( Ignorable by Annotation )

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

49
                return ApiAuth::getInstance(/** @scrutinizer ignore-type */ self::getAuthService(), new Mailer, new Hasher, $jwt);
Loading history...
50
            default :
51
                AuthException::undefinedAuthType();
52
        }
53
    }
54
55
    /**
56
     * Gets the auth service
57
     * @return \Quantum\Mvc\QtService
58
     * @throws \Quantum\Exceptions\ConfigException
59
     */
60
    public static function getAuthService(): QtService
61
    {
62
        return 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

62
        return ServiceFactory::create(/** @scrutinizer ignore-type */ config()->get('auth.service'));
Loading history...
63
    }
64
65
    private static function loadConfigs()
66
    {
67
        if (!config()->has('auth')) {
68
            config()->import(new Setup('Config', 'auth'));
69
        }
70
71
        if (!config()->has('auth.type') && !config()->has('auth.type')) {
72
            throw AuthException::misconfiguredAuthConfig();
73
        }
74
    }
75
76
}
77