Passed
Push — master ( 2c843b...456045 )
by Arman
03:57 queued 14s
created

CaptchaManager::getCaptcha()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 22
rs 9.2222
1
<?php
2
3
namespace Quantum\Libraries\Captcha;
4
5
use Quantum\Exceptions\CaptchaException;
6
use Quantum\Loader\Setup;
7
8
class CaptchaManager
9
{
10
    const ADAPTERS = [
11
        'recaptcha',
12
        'hcaptcha',
13
    ];
14
15
    private static $adapter;
16
17
    public static function getCaptcha() :CaptchaInterface
18
    {
19
        if (self::$adapter !== null) {
20
            return self::$adapter;
21
        }
22
23
        if (!config()->has('captcha')) {
24
            config()->import(new Setup('config', 'captcha'));
25
        }
26
27
        $captchaAdapter = config()->get('captcha.current');
28
29
        if (!in_array($captchaAdapter, self::ADAPTERS) && !is_null($captchaAdapter)) {
30
            throw CaptchaException::unsupportedAdapter($captchaAdapter);
31
        }elseif (is_null($captchaAdapter)){
32
            throw new \Exception('');
33
        }
34
35
        $captchaAdapterClassName = __NAMESPACE__ . '\\Adapters\\' . ucfirst($captchaAdapter) . 'Adapter';
36
        
37
        $params = config()->get('captcha.' . $captchaAdapter);
38
        return self::$adapter = $captchaAdapterClassName::getInstance($params);
39
    }
40
}