Issues (169)

src/Libraries/Auth/AuthManager.php (3 issues)

Labels
Severity
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.0
13
 */
14
15
namespace Quantum\Libraries\Auth;
16
17
use Quantum\Libraries\Mailer\MailerManager;
18
use Quantum\Exceptions\ServiceException;
19
use Quantum\Exceptions\ConfigException;
20
use Quantum\Exceptions\MailerException;
21
use Quantum\Libraries\JWToken\JWToken;
22
use Quantum\Exceptions\AuthException;
23
use Quantum\Exceptions\LangException;
24
use Quantum\Libraries\Hasher\Hasher;
25
use Quantum\Exceptions\DiException;
26
use Quantum\Factory\ServiceFactory;
27
use Quantum\Mvc\QtService;
28
use Quantum\Loader\Setup;
29
use ReflectionException;
30
31
/**
32
 * Class AuthManager
33
 * @package Quantum\Libraries\Auth
34
 */
35
class AuthManager
36
{
37
38
    /**
39
     * Get Handler
40
     * @return ApiAuth|WebAuth|void
41
     * @throws AuthException
42
     * @throws ConfigException
43
     * @throws DiException
44
     * @throws ReflectionException
45
     * @throws ServiceException
46
     * @throws LangException
47
     * @throws MailerException
48
     */
49
    public static function getHandler()
50
    {
51
        self::loadConfigs();
52
53
        if (!config()->has('mailer') || !config()->has('mailer.current')) {
54
            config()->import(new Setup('config', 'mailer'));
55
        }
56
57
        switch (config()->get('auth.type')) {
58
            case 'web':
59
                return WebAuth::getInstance(self::getAuthService(), MailerManager::getHandler(), new Hasher);
0 ignored issues
show
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

59
                return WebAuth::getInstance(/** @scrutinizer ignore-type */ self::getAuthService(), MailerManager::getHandler(), new Hasher);
Loading history...
60
            case 'api':
61
                $jwt = (new JWToken())->setLeeway(1)->setClaims((array)config()->get('auth.claims'));
62
                return ApiAuth::getInstance(self::getAuthService(), MailerManager::getHandler(), new Hasher, $jwt);
0 ignored issues
show
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

62
                return ApiAuth::getInstance(/** @scrutinizer ignore-type */ self::getAuthService(), MailerManager::getHandler(), new Hasher, $jwt);
Loading history...
63
            default:
64
                AuthException::undefinedAuthType();
65
        }
66
    }
67
68
    /**
69
     * Gets the auth service
70
     * @return QtService
71
     * @throws DiException
72
     * @throws ReflectionException
73
     * @throws ServiceException
74
     */
75
    public static function getAuthService(): QtService
76
    {
77
        return ServiceFactory::create(config()->get('auth.service'));
0 ignored issues
show
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

77
        return ServiceFactory::create(/** @scrutinizer ignore-type */ config()->get('auth.service'));
Loading history...
78
    }
79
80
    /**
81
     * @throws ConfigException
82
     * @throws ReflectionException
83
     * @throws DiException
84
     * @throws AuthException
85
     */
86
    private static function loadConfigs()
87
    {
88
        if (!config()->has('auth')) {
89
            config()->import(new Setup('Config', 'auth'));
90
        }
91
92
        if (!config()->has('auth.type') && !config()->has('auth.type')) {
93
            throw AuthException::misconfiguredAuthConfig();
94
        }
95
    }
96
}
97