Passed
Pull Request — master (#135)
by
unknown
03:02
created

CaptchaManager::getCaptcha()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 20
rs 9.9332
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)) {
30
            throw CaptchaException::cantConnect($captchaAdapter);
0 ignored issues
show
Bug introduced by
It seems like $captchaAdapter can also be of type null; however, parameter $name of Quantum\Exceptions\CaptchaException::cantConnect() 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

30
            throw CaptchaException::cantConnect(/** @scrutinizer ignore-type */ $captchaAdapter);
Loading history...
31
        }
32
33
        $captchaAdapterClassName = __NAMESPACE__ . '\\Adapters\\' . ucfirst($captchaAdapter) . 'Adapter';
0 ignored issues
show
Bug introduced by
It seems like $captchaAdapter 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

33
        $captchaAdapterClassName = __NAMESPACE__ . '\\Adapters\\' . ucfirst(/** @scrutinizer ignore-type */ $captchaAdapter) . 'Adapter';
Loading history...
34
        
35
        $params = config()->get('captcha.' . $captchaAdapter);
36
        return self::$adapter = $captchaAdapterClassName::getInstance($params);
37
    }
38
}