DriverFactory::getInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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
 * php version 7.1.0
11
 *
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Firewall\Driver;
24
25
use Shieldon\Firewall\Driver\DriverInterface;
26
use function array_map;
27
use function explode;
28
use function implode;
29
use function ucwords;
30
31
/*
32
 * The factory creates driver instances.
33
 */
34
class DriverFactory
35
{
36
    /**
37
     * Create a driver instance.
38
     *
39
     * @param string $type    The driver's type string.
40
     * @param array  $setting The configuration of that driver.
41
     *
42
     * @return DriverInterface|null
43
     */
44 84
    public static function getInstance(string $type, array $setting)
45
    {
46 84
        $className = '\Shieldon\Firewall\Firewall\Driver\Item' . self::getCamelCase($type) . 'Driver';
47
48 84
        return $className::get($setting);
49
    }
50
51
    /**
52
     * Covert string with dashes into camel-case string.
53
     *
54
     * @param string $string A string with dashes.
55
     *
56
     * @return string
57
     */
58 84
    public static function getCamelCase(string $string = ''): string
59
    {
60 84
        $str = explode('-', $string);
61 84
        $str = implode(
62 84
            '',
63 84
            array_map(
64 84
                function ($word) {
65 84
                    return ucwords($word);
66 84
                },
67 84
                $str
68 84
            )
69 84
        );
70 84
        return $str;
71
    }
72
}
73