Passed
Push — master ( c22b6a...5f79f5 )
by Julito
11:21
created

HookEvent::clearAttachments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * This file contains an abstract Hook event class
7
 * Used for Hook Events (e.g Create user, Webservice registration).
8
 */
9
10
namespace Chamilo\CoreBundle\Hook;
11
12
use Chamilo\CoreBundle\Hook\Interfaces\HookEventInterface;
13
use Chamilo\CoreBundle\Hook\Interfaces\HookObserverInterface;
14
use Doctrine\ORM\EntityManager;
15
16
/**
17
 * Class HookEvent.
18
 *
19
 * This abstract class implements Hook Event Interface to build the base
20
 * for Hook Events. This class have some public static method, e.g for create Hook Events.
21
 */
22
abstract class HookEvent implements HookEventInterface
23
{
24
    public static $hook;
25
    public $observers;
26
    public $eventName;
27
    public $eventData;
28
    public $manager;
29
    protected $entityManager;
30
31
    /**
32
     * Construct Method.
33
     *
34
     * @param string $eventName
35
     */
36
    protected function __construct($eventName, EntityManager $entityManager)
37
    {
38
        $this->entityManager = $entityManager;
39
40
        $this->observers = new \SplObjectStorage();
41
        $this->eventName = $eventName;
42
        $this->eventData = [];
43
        $this->manager = HookManagement::create($this->entityManager);
44
        $this->loadAttachments();
45
    }
46
47
    public function getEntityManager(): EntityManager
48
    {
49
        return $this->entityManager;
50
    }
51
52
    /**
53
     * Load all hook observer already registered from Session or Database.
54
     *
55
     * @return HookEventInterface
56
     */
57
    public function loadAttachments()
58
    {
59
        /*if (isset(self::$hook[$this->eventName]) && is_array(self::$hook[$this->eventName])) {
60
            foreach (self::$hook[$this->eventName] as $hookObserver => $val) {
61
                $hookObserverInstance = $hookObserver::create();
62
                $this->observers->attach($hookObserverInstance);
63
            }
64
        } else {
65
            // Load from Database and save into global name
66
            self::$hook[$this->eventName] = $this->manager->listHookObservers($this->eventName);
67
            if (isset(self::$hook[$this->eventName]) && is_array(self::$hook[$this->eventName])) {
68
                foreach (self::$hook[$this->eventName] as $hookObserver => $val) {
69
                    $hookObserverInstance = $hookObserver::create();
70
                    $this->observers->attach($hookObserverInstance);
71
                }
72
            }
73
        }*/
74
75
        return $this;
76
    }
77
78
    /**
79
     * Return the singleton instance of Hook event.
80
     *
81
     * @return static
82
     */
83
    public static function create(EntityManager $entityManager)
84
    {
85
        static $result = null;
86
87
        if ($result) {
88
            return $result;
89
        }
90
91
        try {
92
            $class = get_called_class();
93
94
            return new $class($entityManager);
95
        } catch (\Exception $e) {
96
            return null;
97
        }
98
    }
99
100
    /**
101
     * Attach an HookObserver.
102
     *
103
     * @see http://php.net/manual/en/splsubject.attach.php
104
     *
105
     * @param HookObserverInterface $observer the HookObserver to attach
106
     */
107
    public function attach(HookObserverInterface $observer)
108
    {
109
        $observerClass = get_class($observer);
110
        self::$hook[$this->eventName][$observerClass] = [
111
            'class_name' => $observerClass,
112
            'path' => $observer->getPath(),
113
            'plugin_name' => $observer->getPluginName(),
114
        ];
115
        $this->observers->attach($observer);
116
        $this->manager->insertHook($this->eventName, $observerClass, HOOK_EVENT_TYPE_ALL);
117
    }
118
119
    /**
120
     * Detach an HookObserver.
121
     *
122
     * @see http://php.net/manual/en/splsubject.detach.php
123
     *
124
     * @param HookObserverInterface $observer The HookObserver to detach
125
     */
126
    public function detach(HookObserverInterface $observer)
127
    {
128
        $observerClass = get_class($observer);
129
        unset(self::$hook[$this->eventName][$observerClass]);
130
        $this->observers->detach($observer);
131
        $this->manager->deleteHook($this->eventName, $observerClass, HOOK_EVENT_TYPE_ALL);
132
    }
133
134
    /**
135
     * Notify an observer.
136
     *
137
     * @see http://php.net/manual/en/splsubject.notify.php
138
     */
139
    public function notify()
140
    {
141
        foreach ($this->observers as $observer) {
142
            $observer->update($this);
143
        }
144
    }
145
146
    /**
147
     * Return the event name refer to where hook is used.
148
     */
149
    public function getEventName(): string
150
    {
151
        return $this->eventName;
152
    }
153
154
    /**
155
     * Return an array containing all data needed by the hook observer to update.
156
     */
157
    public function getEventData(): array
158
    {
159
        return $this->eventData;
160
    }
161
162
    /**
163
     * Set an array with data needed by hooks.
164
     *
165
     * @return $this
166
     */
167
    public function setEventData(array $data): HookEventInterface
168
    {
169
        foreach ($data as $key => $value) {
170
            // Assign value for each array item
171
            $this->eventData[$key] = $value;
172
        }
173
174
        return $this;
175
    }
176
177
    /**
178
     * Detach all hook observers.
179
     */
180
    public function detachAll(): HookEventInterface
181
    {
182
        self::$hook[$this->eventName] = null;
183
        $this->observers->removeAll($this->observers);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Chamilo\CoreBundle\Hook\...aces\HookEventInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
184
    }
185
186
    /**
187
     * Clear all hookObservers without detach them.
188
     */
189
    public function clearAttachments()
190
    {
191
        $this->observers->removeAll($this->observers);
192
    }
193
}
194