Completed
Push — TYPO3_7 ( 85059e...d37b91 )
by Tomas Norre
10:37
created

EventDispatcher   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 19.51%

Importance

Changes 0
Metric Value
dl 0
loc 103
ccs 8
cts 41
cp 0.1951
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 4
A getEvents() 0 4 1
A addObserver() 0 4 1
A hasObserver() 0 4 2
A post() 0 8 3
A getInstance() 0 9 2
1
<?php
2
namespace AOE\Crawler\Event;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * The event dispatcher can be used to register an observer for a
32
 * given event. The observer needs to implement the inferface
33
 * EventObserverInterface
34
 *
35
 * each observer needs to be registered as a TYPO3 Hook.
36
 * Example:
37
 *
38
 * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'][] = 'EXT:aoe_xyz/domain/events/class.tx_xyz_domain_events_crawler.php:tx_xyz_domain_events_crawler';
39
 *
40
 * in the registerObservers the observer can register itself for events:
41
 *
42
 * 	public function registerObservers(EventDispatcher $dispatcher) {
43
 *		$dispatcher->addObserver($this,'addUrl','urlAddedToQueue');
44
 *		$dispatcher->addObserver($this,'duplicateUrlInQueue','duplicateUrlInQueue');
45
 * 		$dispatcher->addObserver($this,'urlCrawled','urlCrawled');
46
 *		$dispatcher->addObserver($this,'invokeQueueChange','invokeQueueChange');
47
 * 		$dispatcher->addObserver($this,'contentChange','contentChange');
48
 *	}
49
 *
50
 * The dispatcher is a singleton. The instance can be retrieved by:
51
 *
52
 * EventDispatcher::getInstance();
53
 *
54
 * Events can be posted by EventDispatcher::getInstance()->post('myEvent','eventGroup', array('foo' => 'bar'));
55
 */
56
class EventDispatcher
57
{
58
    /**
59
     * @var array
60
     */
61
    protected $observers;
62
63
    /**
64
     * @var EventDispatcher
65
     */
66
    protected static $instance;
67
68
    /**
69
     * The __constructor is private because the dispatcher is a singleton
70
     *
71
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
72
     *
73
     * @deprecated since crawler v6.4.0, will be removed in crawler v7.0.0.
74
     */
75
    protected function __construct()
76
    {
77
        GeneralUtility::logDeprecatedFunction();
78
        $this->observers = [];
79
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'])) {
80
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'] as $classRef) {
81
                $hookObj = &GeneralUtility::getUserObj($classRef);
82
                if (method_exists($hookObj, 'registerObservers')) {
83
                    $hookObj->registerObservers($this);
84
                }
85
            }
86
        }
87
    }
88
89
    /**
90
     * Returns all registered event types.
91
     *
92
     * @return array array with registered events.
93
     */
94
    protected function getEvents()
95
    {
96
        return array_keys($this->observers);
97
    }
98
99
    /**
100
     * This method can be used to add an observer for an event to the dispatcher
101
     *
102
     * @param EventObserverInterface $observer_object
103
     * @param string $observer_method
104
     * @param string $event
105
     *
106
     * @return void
107
     */
108 4
    public function addObserver(EventObserverInterface $observer_object, $observer_method, $event)
109
    {
110 4
        $this->observers[$event][] = ['object' => $observer_object, 'method' => $observer_method];
111 4
    }
112
113
    /**
114
     * Enables checking whether a certain event is observed by anyone
115
     *
116
     * @param string $event
117
     *
118
     * @return boolean
119
     */
120
    public function hasObserver($event)
121
    {
122
        return isset($this->observers[$event]) && count($this->observers[$event]) > 0;
123
    }
124
125
    /**
126
     * This method should be used to post a event to the dispatcher. Each
127
     * registered observer will be notified about the event.
128
     *
129
     * @param string $event
130
     * @param string $group
131
     * @param mixed $attachedData
132
     *
133
     * @return void
134
     */
135 4
    public function post($event, $group, $attachedData)
136
    {
137 4
        if (is_array($this->observers[$event])) {
138 4
            foreach ($this->observers[$event] as $eventObserver) {
139 4
                call_user_func([$eventObserver['object'],$eventObserver['method']], $event, $group, $attachedData);
140
            }
141
        }
142 4
    }
143
144
    /**
145
     * Returns the instance of the dispatcher singleton
146
     *
147
     * @return EventDispatcher
148
     */
149
    public static function getInstance()
150
    {
151
        if (!self::$instance instanceof EventDispatcher) {
152
            $dispatcher = new EventDispatcher();
153
            self::$instance = $dispatcher;
154
        }
155
156
        return self::$instance;
157
    }
158
}
159