Completed
Push — master ( b72f37...0c7d03 )
by Alberto
15s
created

PluginHelper::load()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Plugin;
5
6
use Moka\Exception\NotImplementedException;
7
8
/**
9
 * Class PluginHelper
10
 * @package Moka\Plugin
11
 */
12
final class PluginHelper
13
{
14
    /**
15
     * Plugin FQCN has to match this template.
16
     */
17
    const PLUGIN_FQCN_TEMPLATE = 'Moka\\Plugin\\%s\\%sPlugin';
18
19
    /**
20
     * @param string $pluginName
21
     * @return string
22
     *
23
     * @throws NotImplementedException
24
     */
25 2
    public static function load(string $pluginName): string
26
    {
27 2
        $pluginFQCN = self::generateFQCN($pluginName);
28
29
        if (
30 2
            !class_exists($pluginFQCN) ||
31 2
            !in_array(PluginInterface::class, class_implements($pluginFQCN), true)
32
        ) {
33 1
            throw new NotImplementedException(
34 1
                sprintf(
35 1
                    'Mocking strategy "%s" does not exist',
36 1
                    $pluginName
37
                )
38
            );
39
        }
40
41 1
        return $pluginFQCN;
42
    }
43
44
    /**
45
     * @param string $pluginName
46
     * @return string
47
     */
48 2
    private static function generateFQCN(string $pluginName): string
49
    {
50 2
        return sprintf(
51 2
            self::PLUGIN_FQCN_TEMPLATE,
52 2
            ucfirst($pluginName),
53 2
            ucfirst($pluginName)
54
        );
55
    }
56
}
57