Passed
Push — release/1.11.0 ( 94cc26 )
by Schlaefer
02:26
created

AbstractPlugin::initializePlugin()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 46
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5.0031

Importance

Changes 0
Metric Value
eloc 19
nc 8
nop 4
dl 0
loc 46
c 0
b 0
f 0
cc 5
ccs 19
cts 20
cp 0.95
crap 5.0031
rs 8.4751
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\Core\Config;
12
use Phile\Core\Event;
13
use Phile\Core\Utility;
14
use Phile\Gateway\EventObserverInterface;
15
16
/**
17
 * the AbstractPlugin class for implementing a plugin for PhileCMS
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
     * Initializes the 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
     * @param string $pluginDir Root plugin directory this plugin is placed in.
44
     * @param Event $eventBus Phile application event-bus.
45
     * @param Config $config Phile application configuration.
46
     * @return void
47
     */
48 36
    final public function initializePlugin(
49
        string $pluginKey,
50
        string $pluginDir,
51
        Event $eventBus,
52
        Config $config
53
    ): void {
54
        /**
55
         * init $plugin property
56
         */
57 36
        $this->plugin['key'] = $pluginKey;
58 36
        list($vendor, $name) = explode('\\', $this->plugin['key']);
59 36
        $DS = DIRECTORY_SEPARATOR;
60 36
        $this->plugin['dir'] = $pluginDir . $vendor . $DS . $name . $DS;
61
62
        /**
63
         * init events
64
         */
65 36
        foreach ($this->events as $event => $method) {
66 36
            $eventBus->register($event, $this);
67
        }
68
69
        /**
70
         * init plugin settings
71
         */
72 36
        $defaults = Utility::load($this->getPluginPath('config.php'));
73 36
        if (empty($defaults) || !is_array($defaults)) {
74 31
            $defaults = [];
75
        }
76
77 36
        $globals = $config->toArray();
78 36
        if (!isset($globals['plugins'][$pluginKey])) {
79
            $globals['plugins'][$pluginKey] = [];
80
        }
81
82
        // settings precedence: global > default > class
83 36
        $this->settings = array_replace_recursive(
84 36
            $this->settings,
85 36
            $defaults,
86 36
            $globals['plugins'][$pluginKey]
87
        );
88
89
        // backwards compatibility to Phile 1.4
90 36
        $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

90
        /** @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...
91
92 36
        $globals['plugins'][$pluginKey]['settings'] = $this->settings;
93 36
        $config->set($globals);
94 36
    }
95
96
    /**
97
     * inject settings
98
     *
99
     * backwards compatibility to Phile 1.4
100
     *
101
     * @param array $settings
102
     * @return void
103
     * @deprecated since 1.5.1 will be removed
104
     */
105 36
    public function injectSettings(array $settings = null): void
106
    {
107 36
    }
108
109
    /**
110
     * implements EventObserverInterface
111
     *
112
     * @param string $eventKey
113
     * @param null|array $eventData
114
     * @return void
115
     */
116 33
    public function on($eventKey, $eventData = null): void
117
    {
118 33
        if (!isset($this->events[$eventKey])) {
119
            return;
120
        }
121 33
        $method = $this->events[$eventKey];
122 33
        if (!is_callable([$this, $method])) {
123 1
            $class = get_class($this);
124 1
            throw new \RuntimeException(
125 1
                "Event $eventKey can't invoke $class::$method(). Not callable.",
126 1
                1428564865
127
            );
128
        }
129 32
        $this->{$this->events[$eventKey]}($eventData);
130 32
    }
131
132
    /**
133
     * get file path to plugin root (trailing slash) or to a sub-item
134
     *
135
     * @param  string $subPath
136
     * @return string
137
     */
138 36
    protected function getPluginPath(string $subPath = ''): string
139
    {
140 36
        return $this->plugin['dir'] . ltrim($subPath, DIRECTORY_SEPARATOR);
141
    }
142
}
143