Completed
Push — typo3v9 ( aea555...37a7d2 )
by Tomas Norre
06:20
created

EventDispatcher   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 32%

Importance

Changes 0
Metric Value
dl 0
loc 98
ccs 8
cts 25
cp 0.32
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 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
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Event;
6
7
/***************************************************************
8
 *  Copyright notice
9
 *
10
 *  (c) 2017 AOE GmbH <[email protected]>
11
 *
12
 *  All rights reserved
13
 *
14
 *  This script is part of the TYPO3 project. The TYPO3 project is
15
 *  free software; you can redistribute it and/or modify
16
 *  it under the terms of the GNU General Public License as published by
17
 *  the Free Software Foundation; either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  The GNU General Public License can be found at
21
 *  http://www.gnu.org/copyleft/gpl.html.
22
 *
23
 *  This script is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 *  GNU General Public License for more details.
27
 *
28
 *  This copyright notice MUST APPEAR in all copies of the script!
29
 ***************************************************************/
30
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * The event dispatcher can be used to register an observer for a
35
 * given event. The observer needs to implement the inferface
36
 * EventObserverInterface
37
 *
38
 * each observer needs to be registered as a TYPO3 Hook.
39
 * Example:
40
 *
41
 * $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';
42
 *
43
 * in the registerObservers the observer can register itself for events:
44
 *
45
 * 	public function registerObservers(EventDispatcher $dispatcher) {
46
 *		$dispatcher->addObserver($this,'addUrl','urlAddedToQueue');
47
 *		$dispatcher->addObserver($this,'duplicateUrlInQueue','duplicateUrlInQueue');
48
 * 		$dispatcher->addObserver($this,'urlCrawled','urlCrawled');
49
 *		$dispatcher->addObserver($this,'invokeQueueChange','invokeQueueChange');
50
 * 		$dispatcher->addObserver($this,'contentChange','contentChange');
51
 *	}
52
 *
53
 * The dispatcher is a singleton. The instance can be retrieved by:
54
 *
55
 * EventDispatcher::getInstance();
56
 *
57
 * Events can be posted by EventDispatcher::getInstance()->post('myEvent','eventGroup', array('foo' => 'bar'));
58
 */
59
class EventDispatcher
60
{
61
    /**
62
     * @var array
63
     */
64
    protected $observers;
65
66
    /**
67
     * @var EventDispatcher
68
     */
69
    protected static $instance;
70
71
    /**
72
     * The __constructor is private because the dispatcher is a singleton
73
     */
74
    protected function __construct()
75
    {
76
        $this->observers = [];
77
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'])) {
78
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'] as $classRef) {
79
                $hookObj = &GeneralUtility::makeInstance($classRef);
80
                if (method_exists($hookObj, 'registerObservers')) {
81
                    $hookObj->registerObservers($this);
82
                }
83
            }
84
        }
85
    }
86
87
    /**
88
     * Returns all registered event types.
89
     *
90
     * @return array array with registered events.
91
     */
92
    protected function getEvents()
93
    {
94
        return array_keys($this->observers);
95
    }
96
97
    /**
98
     * This method can be used to add an observer for an event to the dispatcher
99
     *
100
     * @param EventObserverInterface $observer_object
101
     * @param string $observer_method
102
     * @param string $event
103
     *
104
     * @return void
105
     */
106 4
    public function addObserver(EventObserverInterface $observer_object, $observer_method, $event): void
107
    {
108 4
        $this->observers[$event][] = ['object' => $observer_object, 'method' => $observer_method];
109 4
    }
110
111
    /**
112
     * Enables checking whether a certain event is observed by anyone
113
     *
114
     * @param string $event
115
     *
116
     * @return boolean
117
     */
118
    public function hasObserver($event)
119
    {
120
        return isset($this->observers[$event]) && count($this->observers[$event]) > 0;
121
    }
122
123
    /**
124
     * This method should be used to post a event to the dispatcher. Each
125
     * registered observer will be notified about the event.
126
     *
127
     * @param string $event
128
     * @param string $group
129
     * @param mixed $attachedData
130
     *
131
     * @return void
132
     */
133 4
    public function post($event, $group, $attachedData): void
134
    {
135 4
        if (is_array($this->observers[$event])) {
136 4
            foreach ($this->observers[$event] as $eventObserver) {
137 4
                call_user_func([$eventObserver['object'], $eventObserver['method']], $event, $group, $attachedData);
138
            }
139
        }
140 4
    }
141
142
    /**
143
     * Returns the instance of the dispatcher singleton
144
     *
145
     * @return EventDispatcher
146
     */
147
    public static function getInstance()
148
    {
149
        if (!self::$instance instanceof EventDispatcher) {
150
            $dispatcher = new EventDispatcher();
151
            self::$instance = $dispatcher;
152
        }
153
154
        return self::$instance;
155
    }
156
}
157