Plugin   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 61
c 2
b 1
f 0
dl 0
loc 160
rs 10
wmc 25

7 Methods

Rating   Name   Duplication   Size   Complexity  
A removeOnEvent() 0 4 1
A loadRelatedData() 0 19 3
A getCurrentVersion() 0 6 1
A attachOnEvent() 0 8 1
A seedRelated() 0 16 3
B attachRelatedPiecesAfterSave() 0 38 11
A setRelatedData() 0 14 5
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jgulledge
5
 * Date: 9/30/2017
6
 * Time: 2:44 PM
7
 */
8
9
namespace LCI\Blend\Blendable;
10
11
12
class Plugin extends Element
13
{
14
    /** @var string  */
15
    protected $opt_cache_key = 'elements/plugins';
16
17
    /** @var string ~ the xPDO class name */
18
    protected $xpdo_simple_object_class = 'modPlugin';
19
20
    /** @var array  */
21
    protected $on_event_names = [];
22
23
    /** @var array  */
24
    protected $remove_on_event_names = [];
25
26
    /**
27
     * @return \LCI\Blend\Blendable\Plugin
28
     */
29
    public function getCurrentVersion()
30
    {
31
        /** @var \LCI\Blend\Blendable\Plugin $plugin */
32
        $plugin = new self($this->modx, $this->blender, $this->getFieldName());
33
        return $plugin
34
            ->setSeedsDir($this->getSeedsDir());
35
    }
36
37
    /**
38
     * @param string $event_name
39
     * @param int $priority
40
     * @param int $property_set
41
     * @return $this
42
     */
43
    public function attachOnEvent($event_name, $priority = 0, $property_set = 0)
44
    {
45
        $this->related_data[] = [
46
            'event' => $event_name,
47
            'priority' => $priority,
48
            'propertyset' => $property_set
49
        ];
50
        return $this;
51
    }
52
53
    /**
54
     * @param string $event_name
55
     *
56
     * @return $this
57
     */
58
    public function removeOnEvent($event_name)
59
    {
60
        $this->remove_on_event_names[] = $event_name;
61
        return $this;
62
    }
63
    /**
64
     * Override in child classes
65
     */
66
    protected function loadRelatedData()
67
    {
68
        // get all related Events:
69
        $events = [];
70
        if ($this->xPDOSimpleObject instanceof \modPlugin) {
0 ignored issues
show
Bug introduced by
The type modPlugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
            $pluginEvents = $this->xPDOSimpleObject->getMany('PluginEvents');
72
            /** @var \modPluginEvent $event */
73
            foreach ($pluginEvents as $event) {
74
                $data = $event->toArray();
75
                unset($data['id'], $data['pluginid']);
76
                $events[] = $data;
77
            }
78
79
            // will be loaded via setOnEvents from blend()
80
            $this->related_data = $events;
81
        }
82
83
        // Calls on the event: OnBlendLoadRelatedData
84
        parent::loadRelatedData();
85
    }
86
87
    protected function attachRelatedPiecesAfterSave()
88
    {
89
        // remove any:
90
        if (count($this->remove_on_event_names) > 0) {
91
            $removePluginEvents = $this->xPDOSimpleObject->getMany('PluginEvents', ['event:IN' => $this->remove_on_event_names]);
0 ignored issues
show
Bug introduced by
The method getMany() does not exist on null. ( Ignorable by Annotation )

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

91
            /** @scrutinizer ignore-call */ 
92
            $removePluginEvents = $this->xPDOSimpleObject->getMany('PluginEvents', ['event:IN' => $this->remove_on_event_names]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
            /** @var \modPluginEvent $event */
93
            foreach ($removePluginEvents as $event) {
94
                if (!$event->remove()) {
95
                    $this->blender->out('Plugin did not detach the event: '.$event->get('event'));
96
                }
97
            }
98
            $this->xPDOSimpleObject->save();
99
        }
100
101
        if (count($this->related_data) > 0) {
102
            foreach ($this->related_data as $event_data) {
103
                if (in_array($event_data['event'], $this->remove_on_event_names)) {
104
                    continue;
105
                }
106
107
                // Does it already exist?
108
                $pluginEvent = $this->modx->getObject('modPluginEvent', [
109
                    'event' => $event_data['event'],
110
                    'pluginid' => $this->xPDOSimpleObject->get('id')
111
                ]);
112
113
                if (!$pluginEvent) {
114
                    $pluginEvent = $this->modx->newObject('modPluginEvent');
115
                    $pluginEvent->set('event', $event_data['event']);
116
                    $pluginEvent->set('pluginid', $this->xPDOSimpleObject->get('id'));
117
                }
118
119
                $priority = (!empty($event_data['priority']) ? $event_data['priority'] : 0);
120
                $pluginEvent->set('priority', (int)$priority);
121
                $pluginEvent->set('propertyset', (int)(!empty($event_data['propertyset']) ? $event_data['propertyset'] : 0));
122
123
                if (!$pluginEvent->save()) {
124
                    $this->blender->out('Plugin did not attached the event: '.$event_data['event']);
125
                }
126
            }
127
        }
128
    }
129
130
    /**
131
     * Called from loadFromArray(), for build from seeds
132
     *
133
     * @param mixed|array $data
134
     *
135
     * @return $this
136
     */
137
    public function setRelatedData($data)
138
    {
139
        if (is_array($data)) {
140
            foreach ($data as $count => $event) {
141
                if (isset($event['remove']) && $event['remove']) {
142
                    $this->removeOnEvent($event['event']);
143
144
                } else {
145
                    $this->attachOnEvent($event['event'], $event['priority'], $event['propertyset']);
146
                }
147
            }
148
        }
149
150
        return $this;
151
    }
152
153
    /**
154
     * @var string $type blend or revert
155
     */
156
    protected function seedRelated($type = 'blend')
157
    {
158
        // get all related Events:
159
        $events = [];
160
        if (is_object($this->xPDOSimpleObject)) {
161
            $pluginEvents = $this->xPDOSimpleObject->getMany('PluginEvents');
162
            /** @var \modPluginEvent $event */
163
            foreach ($pluginEvents as $event) {
164
                $data = $event->toArray();
165
                unset($data['id'], $data['pluginid']);
166
                $events[] = $data;
167
168
            }
169
        }
170
        // will be loaded via setOnEvents from blend()
171
        $this->related_data = $events;
172
    }
173
}
174