Passed
Push — 2.x ( a2237c...c5863b )
by Terry
02:56
created

CaptchaFactory::check()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Firewall\Captcha;
14
15
use Shieldon\Firewall\Captcha\CaptchaInterface;
16
17
/*
18
 * The factory creates driver instances.
19
 */
20
class CaptchaFactory
21
{
22
    /**
23
     * Create a captcha instance.
24
     *
25
     * @param string $type    The driver's type string.
26
     * @param array  $setting The configuration of that driver.
27
     *
28
     * @return CaptchaInterface
29
     */
30
    public static function getInstance(string $type, array $setting): CaptchaInterface
31
    {
32
        $className = '\Shieldon\Firewall\Firewall\Captcha\Item' . self::getCamelCase($type);
33
34
        return $className::get($setting);
35
    }
36
37
    /**
38
     * Check whether a messenger is available or not.
39
     *
40
     * @param string $type    The messenger's ID string.
41
     * @param array  $setting The configuration of that messanger.
42
     *
43
     * @return bool
44
     */
45
    public static function check(string $type, array $setting): bool
46
    {
47
        if (empty($setting['enable'])) {
48
            return false;
49
        }
50
51
        // If the class doesn't exist.
52
        if (!file_exists(__DIR__ . '/' . self::getCamelCase($type) . '.php')) {
53
            return false;
54
        }
55
56
        return true;
57
    }
58
59
    /**
60
     * Covert string with dashes into camel-case string.
61
     *
62
     * @param string $string A string with dashes.
63
     *
64
     * @return string
65
     */
66
    public static function getCamelCase(string $string = '')
67
    {
68
        $str = explode('-', $string);
69
        $str = implode('', array_map(function($word) {
70
            return ucwords($word); 
71
        }, $str));
72
73
        return $str;
74
    }
75
}