Passed
Branch master (748c89)
by Schlaefer
02:28
created

AbstractPlugin::initializePlugin()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 42
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5.0031

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 8
nop 1
dl 0
loc 42
ccs 19
cts 20
cp 0.95
crap 5.0031
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plugin class
4
 */
5
namespace Phile\Plugin;
6
7
use Phile\Core\Container;
8
use Phile\Core\Utility;
9
use Phile\Gateway\EventObserverInterface;
10
11
/**
12
 * the AbstractPlugin class for implementing a plugin for PhileCMS
13
 *
14
 * @author  PhileCMS
15
 * @link    https://philecms.com
16
 * @license http://opensource.org/licenses/MIT
17
 * @package Phile\Plugin
18
 */
19
abstract class AbstractPlugin implements EventObserverInterface
20
{
21
    /**
22
     * @var array plugin attributes
23
     */
24
    private $plugin = [];
25
26
    /**
27
     * @var array subscribed Phile events ['eventName' => 'classMethodToCall']
28
     */
29
    protected $events = [];
30
31
    /**
32
     * @var array the plugin settings
33
     */
34
    protected $settings = [];
35
36
    /**
37
     * initialize plugin
38
     *
39
     * try to keep all initialization in one method to have a clean class
40
     * for the plugin-user
41
     *
42
     * @param      string $pluginKey
43
     * @deprecated since 1.5.1 will be declared 'final'
44
     */
45 33
    public function initializePlugin($pluginKey)
46
    {
47
        /**
48
         * init $plugin property
49
         */
50 33
        $this->plugin['key'] = $pluginKey;
51 33
        list($vendor, $name) = explode('\\', $this->plugin['key']);
52 33
        $DS = DIRECTORY_SEPARATOR;
53 33
        $this->plugin['dir'] = PLUGINS_DIR . $vendor . $DS . $name . $DS;
54
55
        /**
56
         * init events
57
         */
58 33
        foreach ($this->events as $event => $method) {
59 33
            Container::getInstance()->get('Phile_EventBus')->register($event, $this);
60
        }
61
62
        /**
63
         * init plugin settings
64
         */
65 33
        $defaults = Utility::load($this->getPluginPath('config.php'));
66 33
        if (empty($defaults) || !is_array($defaults)) {
67 28
            $defaults = [];
68
        }
69
70 33
        $globals = Container::getInstance()->get('Phile_Config')->toArray();
71 33
        if (!isset($globals['plugins'][$pluginKey])) {
72
            $globals['plugins'][$pluginKey] = [];
73
        }
74
75
        // settings precedence: global > default > class
76 33
        $this->settings = array_replace_recursive(
77 33
            $this->settings,
78 33
            $defaults,
79 33
            $globals['plugins'][$pluginKey]
80
        );
81
82
        // backwards compatibility to Phile 1.4
83 33
        $this->injectSettings($this->settings);
0 ignored issues
show
Deprecated Code introduced by
The function Phile\Plugin\AbstractPlugin::injectSettings() has been deprecated: since 1.5.1 will be removed ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

83
        /** @scrutinizer ignore-deprecated */ $this->injectSettings($this->settings);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
84
85 33
        $globals['plugins'][$pluginKey]['settings'] = $this->settings;
86 33
        Container::getInstance()->get('Phile_Config')->set($globals);
87 33
    }
88
89
    /**
90
     * inject settings
91
     *
92
     * backwards compatibility to Phile 1.4
93
     *
94
     * @param      array $settings
95
     * @deprecated since 1.5.1 will be removed
96
     */
97 33
    public function injectSettings(array $settings = null)
98
    {
99 33
    }
100
101
    /**
102
     * implements EventObserverInterface
103
     *
104
     * @param  string $eventKey
105
     * @param  null   $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
106
     * @return void
107
     */
108 30
    public function on($eventKey, $data = null)
109
    {
110 30
        if (!isset($this->events[$eventKey])) {
111
            return;
112
        }
113 30
        $method = $this->events[$eventKey];
114 30
        if (!is_callable([$this, $method])) {
115 1
            $class = get_class($this);
116 1
            throw new \RuntimeException(
117 1
                "Event $eventKey can't invoke $class::$method(). Not callable.",
118 1
                1428564865
119
            );
120
        }
121 29
        $this->{$this->events[$eventKey]}($data);
122 29
    }
123
124
    /**
125
     * get file path to plugin root (trailing slash) or to a sub-item
126
     *
127
     * @param  string $subPath
128
     * @return string
129
     */
130 33
    protected function getPluginPath(string $subPath = ''): string
131
    {
132 33
        return $this->plugin['dir'] . ltrim($subPath, DIRECTORY_SEPARATOR);
133
    }
134
}
135