Completed
Pull Request — master (#12)
by Angelo
08:49
created

PluginHelper::generatePluginFQCN()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Plugin;
5
6
use Moka\Exception\NotImplementedException;
7
8
class PluginHelper
9
{
10
    /**
11
     * The base namespace with plugin must be declared
12
     */
13
    const PLUGIN_NAMESPACE_TEMPLATE = 'Moka\\Plugin\\%s\\%sPlugin';
14
15
    /**
16
     * @param string $pluginName
17
     * @return string
18
     */
19
    public static function generatePluginFQCN(string $pluginName): string
20
    {
21
        return sprintf(
22
            self::PLUGIN_NAMESPACE_TEMPLATE,
23
            ucfirst($pluginName),
24
            ucfirst($pluginName)
25
        );
26
    }
27
28
    /**
29
     * @param string $pluginName
30
     * @return string
31
     * @throws NotImplementedException
32
     */
33
    public static function loadPlugin(string $pluginName): string
34
    {
35
        $pluginFQCN = PluginHelper::generatePluginFQCN($pluginName);
36
37
        if (!class_exists($pluginFQCN) || !in_array(PluginInterface::class, class_implements($pluginFQCN), true)) {
38
            throw new NotImplementedException(
39
                sprintf(
40
                    'Mocking strategy "%s" does not exist',
41
                    $pluginName
42
                )
43
            );
44
        }
45
46
        return $pluginFQCN;
47
    }
48
}
49