Completed
Push — master ( d9f04e...3eb62a )
by Arman
21s queued 11s
created

AuthManager::getHandler()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 19
rs 9.5222
cc 5
nc 4
nop 1
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.0.0
13
 */
14
15
namespace Quantum\Libraries\Auth;
16
17
use Quantum\Exceptions\ExceptionMessages;
18
use Quantum\Libraries\JWToken\JWToken;
19
use Quantum\Exceptions\AuthException;
20
use Quantum\Libraries\Mailer\Mailer;
21
use Quantum\Libraries\Hasher\Hasher;
22
use Quantum\Factory\ServiceFactory;
23
use Quantum\Loader\Loader;
24
use Quantum\Di\Di;
25
use stdClass;
26
27
/**
28
 * Class AuthManager
29
 * @package Quantum\Libraries\Auth
30
 */
31
class AuthManager
32
{
33
34
    /**
35
     * @var AuthenticableInterface
36
     */
37
    private static $authInstance = null;
38
39
    /**
40
     * @var string
41
     */
42
    private static $authType = null;
43
44
    /**
45
     * GetHandler
46
     * @return WebAuth|ApiAuth
47
     * @throws AuthException
48
     */
49
    public static function getHandler(Loader $loader)
50
    {
51
        $authService = self::authService($loader);
52
53
        if (self::$authType && $authService) {
54
            switch (self::$authType) {
55
                case 'web':
56
                    self::$authInstance = new WebAuth($authService, new Mailer, new Hasher);
57
                    break;
58
                case 'api':
59
                    $jwt = (new JWToken())->setLeeway(1)->setClaims((array) config()->get('auth.claims'));
60
                    self::$authInstance = new ApiAuth($authService, new Mailer, new Hasher, $jwt);
61
                    break;
62
            }
63
        } else {
64
            throw new AuthException(ExceptionMessages::MISCONFIGURED_AUTH_CONFIG);
65
        }
66
67
        return self::$authInstance;
68
    }
69
70
    /**
71
     * Auth Service
72
     * @param Loader $loader
73
     * @return AuthServiceInterface
74
     * @throws \Exception
75
     */
76
    public static function authService(Loader $loader): AuthServiceInterface
77
    {
78
        if (!config()->has('auth')) {
79
80
            $loaderSetup = new stdClass();
81
            $loaderSetup->module = current_module();
82
            $loaderSetup->env = 'config';
83
            $loaderSetup->fileName = 'auth';
84
            $loaderSetup->exceptionMessage = ExceptionMessages::CONFIG_FILE_NOT_FOUND;
85
86
            $loader->setup($loaderSetup);
87
88
            config()->import($loader, 'auth');
89
        }
90
91
        self::$authType = config()->get('auth.type');
92
93
        return Di::get(ServiceFactory::class)->create(config()->get('auth.service'));
94
    }
95
96
}
97