Passed
Push — master ( 1282f8...92a01c )
by Schlaefer
03:31
created

AbstractPlugin::on()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

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

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

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

Loading history...
86
87 7
        $globals['plugins'][$pluginKey]['settings'] = $this->settings;
88 7
        Registry::set('Phile_Settings', $globals);
89 7
    }
90
91
    /**
92
     * inject settings
93
     *
94
     * backwards compatibility to Phile 1.4
95
     *
96
     * @param      array $settings
97
     * @deprecated since 1.5.1 will be removed
98
     */
99 7
    public function injectSettings(array $settings = null)
0 ignored issues
show
Unused Code introduced by
The parameter $settings is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101 7
    }
102
103
    /**
104
     * implements EventObserverInterface
105
     *
106
     * @param  string $eventKey
107
     * @param  null   $data
108
     * @return void
109
     */
110 5
    public function on($eventKey, $data = null)
111
    {
112 5
        if (!isset($this->events[$eventKey])) {
113
            return;
114
        }
115 5
        $method = $this->events[$eventKey];
116 5
        if (!is_callable([$this, $method])) {
117 1
            $class = get_class($this);
118 1
            throw new \RuntimeException(
119 1
                "Event $eventKey can't invoke $class::$method(). Not callable.",
120 1
                1428564865
121
            );
122
        }
123 4
        $this->{$this->events[$eventKey]}($data);
124 4
    }
125
126
    /**
127
     * get file path to plugin root (trailing slash) or to a sub-item
128
     *
129
     * @param  string $subPath
130
     * @return null|string null if item does not exist
131
     */
132 8
    protected function getPluginPath($subPath = '')
133
    {
134 8
        return $this->plugin['dir'] . ltrim($subPath, DIRECTORY_SEPARATOR);
135
    }
136
}
137