Passed
Push — 2.x ( 75d9da...077994 )
by Terry
01:57
created

MessengerFactory::getCamelCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
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\Messenger;
14
15
use Shieldon\Messenger\Messenger\MessengerInterface;
16
17
/*
18
 * The factory creates messenger instances.
19
 */
20
class MessengerFactory
21
{
22
    /**
23
     * Create a messenger instance.
24
     *
25
     * @param string $messenger The messenger's ID string.
26
     * @param array  $setting   The configuration of that messanger.
27
     *
28
     * @return MessengerInterface
29
     */
30
    public static function getInstance(string $messenger, array $setting): MessengerInterface
31
    {
32
        $className = 'Item' . self::getCamelCase($messenger);
33
34
        return $className::get($setting);
35
    }
36
37
    /**
38
     * Check whether a messenger is available or not.
39
     *
40
     * @param string $messenger 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 $messenger, array $setting): bool
46
    {
47
        // If the settings is not set correctly.
48
        if (empty($setting['enable']) || empty($setting['confirm_test'])) {
49
            return false;
50
        }
51
52
        // If the class doesn't exist.
53
        if (!file_exists(__DIR__ . '/' . self::getCamelCase($messenger) . '.php')) {
54
            return false;
55
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 53 is false. This is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
56
    }
57
58
    /**
59
     * Covert string with dashes into camel-case string.
60
     *
61
     * @param string $string A string with dashes.
62
     *
63
     * @return string
64
     */
65
    public static function getCamelCase(string $string = '')
66
    {
67
        $str = explode('-', $string);
68
        $str = implode('', array_map(function($word) {
69
            return ucwords($word); 
70
        }, $str));
71
72
        return $str;
73
    }
74
}