Passed
Pull Request — master (#143)
by Arman
05:04 queued 02:36
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\Exceptions\ConfigException;
7
use Quantum\Libraries\Curl\HttpClient;
8
use Quantum\Exceptions\DiException;
9
use Quantum\Loader\Setup;
10
use ReflectionException;
11
12
class CaptchaManager
13
{
14
    const ADAPTERS = [
15
        'recaptcha',
16
        'hcaptcha',
17
    ];
18
19
    private static $adapter;
20
21
    /**
22
     * @return CaptchaInterface
23
     * @throws CaptchaException
24
     * @throws ConfigException
25
     * @throws DiException
26
     * @throws ReflectionException
27
     */
28
    public static function getHandler(): CaptchaInterface
29
    {
30
        if (self::$adapter !== null) {
31
            return self::$adapter;
32
        }
33
34
        if (!config()->has('captcha')) {
35
            config()->import(new Setup('config', 'captcha'));
36
        }
37
38
        $captchaAdapter = config()->get('captcha.current');
39
40
        if (!in_array($captchaAdapter, self::ADAPTERS)) {
41
            throw CaptchaException::unsupportedAdapter($captchaAdapter);
0 ignored issues
show
Bug introduced by
It seems like $captchaAdapter can also be of type null; however, parameter $name of Quantum\Exceptions\Captc...n::unsupportedAdapter() 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

41
            throw CaptchaException::unsupportedAdapter(/** @scrutinizer ignore-type */ $captchaAdapter);
Loading history...
42
        }
43
44
        $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

44
        $captchaAdapterClassName = __NAMESPACE__ . '\\Adapters\\' . ucfirst(/** @scrutinizer ignore-type */ $captchaAdapter) . 'Adapter';
Loading history...
45
46
        return self::$adapter = $captchaAdapterClassName::getInstance(config()->get('captcha.' . $captchaAdapter), new HttpClient);
47
    }
48
}