Passed
Pull Request — master (#190)
by Arman
02:52
created

CaptchaFactory::createInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
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.5
13
 */
14
15
namespace Quantum\Libraries\Captcha\Factories;
16
17
use Quantum\Libraries\Captcha\Exceptions\CaptchaException;
18
use Quantum\Libraries\Captcha\Adapters\RecaptchaAdapter;
19
use Quantum\Libraries\Config\Exceptions\ConfigException;
20
use Quantum\Libraries\Captcha\Adapters\HcaptchaAdapter;
21
use Quantum\Libraries\HttpClient\HttpClient;
22
use Quantum\Di\Exceptions\DiException;
23
use Quantum\Libraries\Captcha\Captcha;
24
use Quantum\Exceptions\BaseException;
25
use Quantum\Loader\Setup;
26
use ReflectionException;
27
28
/**
29
 * Class CaptchaFactory
30
 * @package Quantum\Libraries\Captcha
31
 */
32
class CaptchaFactory
33
{
34
35
    /**
36
     * Supported adapters
37
     */
38
    const ADAPTERS = [
39
        Captcha::HCAPTCHA => HcaptchaAdapter::class,
40
        Captcha::RECAPTCHA => RecaptchaAdapter::class,
41
    ];
42
43
    /**
44
     * @var Captcha|null
45
     */
46
    private static $instance = null;
47
48
    /**
49
     * @return Captcha
50
     * @throws BaseException
51
     * @throws ConfigException
52
     * @throws DiException
53
     * @throws ReflectionException
54
     */
55
    public static function get(): Captcha
56
    {
57
        if (self::$instance === null) {
58
            return self::$instance = self::createInstance();
59
        }
60
61
        return self::$instance;
62
    }
63
64
    /**
65
     * @return Captcha
66
     * @throws BaseException
67
     * @throws ConfigException
68
     * @throws DiException
69
     * @throws ReflectionException
70
     */
71
    private static function createInstance(): Captcha
72
    {
73
        if (!config()->has('captcha')) {
74
            config()->import(new Setup('config', 'captcha'));
75
        }
76
77
        $adapter = config()->get('captcha.current');
78
79
        $adapterClass = self::getAdapterClass($adapter);
0 ignored issues
show
Bug introduced by
It seems like $adapter can also be of type null; however, parameter $adapter of Quantum\Libraries\Captch...tory::getAdapterClass() 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

79
        $adapterClass = self::getAdapterClass(/** @scrutinizer ignore-type */ $adapter);
Loading history...
80
81
        return new Captcha(new $adapterClass(config()->get('captcha.' . $adapter), new HttpClient()));
82
    }
83
84
    /**
85
     * @param string $adapter
86
     * @return string
87
     * @throws BaseException
88
     */
89
    private static function getAdapterClass(string $adapter): string
90
    {
91
        if (!array_key_exists($adapter, self::ADAPTERS)) {
92
            throw CaptchaException::adapterNotSupported($adapter);
93
        }
94
95
        return self::ADAPTERS[$adapter];
96
    }
97
}