Completed
Pull Request — master (#44)
by Alberto
02:53
created

loadPlugin.php ➔ loadPlugin()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

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