Passed
Push — develop ( ce9409...4d02c5 )
by Schlaefer
35s
created

PluginDirectory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 91
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getPath() 0 4 1
A newPluginInstance() 0 18 3
A autoload() 0 10 3
A filenameForClass() 0 13 1
1
<?php
2
/**
3
 * @author PhileCMS
4
 * @link https://philecms.github.io/
5
 * @license http://opensource.org/licenses/MIT
6
 * @package Phile\Plugin
7
 */
8
9
namespace Phile\Plugin;
10
11
use Phile\Exception\PluginException;
12
13
/**
14
 * Represents a dire directory with plugin folders
15
 */
16
class PluginDirectory
17
{
18
    /**
19
     * File path to directory
20
     *
21
     * @var string
22
     */
23
    protected $path;
24
25
    /**
26
     * Constructor
27
     *
28
     * @param string $path
29
     */
30 36
    public function __construct(string $path)
31
    {
32 36
        $this->path = $path;
33 36
        spl_autoload_register([$this, 'autoload']);
34 36
    }
35
36
    /**
37
     * Gets directory path
38
     *
39
     * @return string path
40
     */
41 34
    public function getPath(): string
42
    {
43 34
        return $this->path;
44
    }
45
46
    /**
47
     * Tries to create new a Plugin class from plugin in directory
48
     *
49
     * @param string $pluginKey
50
     * @return AbstractPlugin|null
51
     */
52 32
    public function newPluginInstance(string $pluginKey): ?AbstractPlugin
53
    {
54 32
        list($vendor, $pluginName) = explode('\\', $pluginKey);
55
        // uppercase first letter convention
56 32
        $className = 'Phile\\Plugin\\' . ucfirst($vendor) . '\\' . ucfirst($pluginName) . '\\Plugin';
57 32
        if (!file_exists($this->filenameForClass($className))) {
58 1
            return null;
59
        };
60
61 31
        $plugin = new $className;
62 31
        if (($plugin instanceof AbstractPlugin) === false) {
63 1
            throw new PluginException(
64 1
                "the plugin '{$pluginKey}' is not an instance of \\Phile\\Plugin\\AbstractPlugin",
65 1
                1398536526
66
            );
67
        }
68 30
        return $plugin;
69
    }
70
71
    /**
72
     * Class auto-loader plugin namespace
73
     *
74
     * @param string $className
75
     */
76 31
    public function autoload(string $className)
77
    {
78 31
        if (strpos($className, "Phile\\Plugin\\") !== 0) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Phile\\Plugin\\ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
79 29
            return;
80
        }
81 3
        $filename = $this->filenameForClass($className);
82 3
        if (file_exists($filename)) {
83 3
            include $filename;
84
        }
85 3
    }
86
87
    /**
88
     * Creates file-path to class-file in plugin directory
89
     *
90
     * @param string $className
91
     * @return string the file path
92
     */
93 33
    protected function filenameForClass(string $className): string
94
    {
95 33
        $className = str_replace('Phile\\Plugin\\', '', $className);
96 33
        $classNameParts = explode('\\', $className);
97 33
        $pluginVendor = lcfirst(array_shift($classNameParts));
98 33
        $pluginName = lcfirst(array_shift($classNameParts));
99 33
        $classPath = array_merge(
100 33
            [$pluginVendor, $pluginName, 'Classes'],
101 33
            $classNameParts
102
        );
103
104 33
        return $this->path . implode('/', $classPath) . '.php';
105
    }
106
}
107