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

PluginHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generatePluginFQCN() 0 8 1
A loadPlugin() 0 15 3
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