Passed
Push — deprecations ( 0237b0...c7191d )
by Tomas Norre
06:21
created

EventDispatcher::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 4
nop 0
dl 0
loc 9
ccs 4
cts 8
cp 0.5
crap 6
rs 10
c 1
b 0
f 0
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\Compatibility\PublicMethodDeprecationTrait;
32
use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35
/**
36
 * The event dispatcher can be used to register an observer for a
37
 * given event. The observer needs to implement the inferface
38
 * EventObserverInterface
39
 *
40
 * each observer needs to be registered as a TYPO3 Hook.
41
 * Example:
42
 *
43
 * $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';
44
 *
45
 * in the registerObservers the observer can register itself for events:
46
 *
47
 * 	public function registerObservers(EventDispatcher $dispatcher) {
48
 *		$dispatcher->addObserver($this,'addUrl','urlAddedToQueue');
49
 *		$dispatcher->addObserver($this,'duplicateUrlInQueue','duplicateUrlInQueue');
50
 * 		$dispatcher->addObserver($this,'urlCrawled','urlCrawled');
51
 *		$dispatcher->addObserver($this,'invokeQueueChange','invokeQueueChange');
52
 * 		$dispatcher->addObserver($this,'contentChange','contentChange');
53
 *	}
54
 *
55
 * The dispatcher is a singleton. The instance can be retrieved by:
56
 *
57
 * EventDispatcher::getInstance();
58
 *
59
 * Events can be posted by EventDispatcher::getInstance()->post('myEvent','eventGroup', array('foo' => 'bar'));
60
 *
61
 * @deprecated Is deprecated since 9.0.1 and will be removed in v10.x
62
 */
63
class EventDispatcher
64
{
65
    /**
66
     * @var array
67
     */
68
    protected $observers;
69
70
    /**
71
     * @var EventDispatcher
72
     */
73
    protected static $instance;
74
75
    /**
76
     * The __constructor is private because the dispatcher is a singleton
77
     */
78 16
    protected function __construct()
79
    {
80 16
        trigger_error('The ' . __CLASS__ . ' is deprecated an will be removed in v10.x, will be migrated to PSR-14 EventDispatcher');
81 16
        $this->observers = [];
82 16
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'])) {
83
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'] as $classRef) {
84
                $hookObj = &GeneralUtility::makeInstance($classRef);
85
                if (method_exists($hookObj, 'registerObservers')) {
86
                    $hookObj->registerObservers($this);
87
                }
88
            }
89
        }
90 16
    }
91
92
    /**
93
     * Returns all registered event types.
94
     *
95
     * @return array array with registered events.
96
     * @deprecated
97
     */
98
    protected function getEvents()
99
    {
100
        return array_keys($this->observers);
101
    }
102
103
    /**
104
     * This method can be used to add an observer for an event to the dispatcher
105
     *
106
     * @param EventObserverInterface $observer_object
107
     * @param string $observer_method
108
     * @param string $event
109
     *
110
     * @return void
111
     * @deprecated
112
     */
113 4
    public function addObserver(EventObserverInterface $observer_object, $observer_method, $event): void
114
    {
115 4
        $this->observers[$event][] = ['object' => $observer_object, 'method' => $observer_method];
116 4
    }
117
118
    /**
119
     * Enables checking whether a certain event is observed by anyone
120
     *
121
     * @param string $event
122
     *
123
     * @return boolean
124
     * @deprecated
125
     */
126 11
    public function hasObserver($event)
127
    {
128 11
        return isset($this->observers[$event]) && count($this->observers[$event]) > 0;
129
    }
130
131
    /**
132
     * This method should be used to post a event to the dispatcher. Each
133
     * registered observer will be notified about the event.
134
     *
135
     * @param string $event
136
     * @param string $group
137
     * @param mixed $attachedData
138
     *
139
     * @return void
140
     * @deprecated
141
     */
142 9
    public function post($event, $group, $attachedData): void
143
    {
144 9
        if (is_array($this->observers[$event])) {
145 4
            foreach ($this->observers[$event] as $eventObserver) {
146 4
                call_user_func([$eventObserver['object'], $eventObserver['method']], $event, $group, $attachedData);
147
            }
148
        }
149 9
    }
150
151
    /**
152
     * Returns the instance of the dispatcher singleton
153
     *
154
     * @return EventDispatcher
155
     * @deprecated
156
     */
157 16
    public static function getInstance()
158
    {
159 16
        if (!self::$instance instanceof EventDispatcher) {
0 ignored issues
show
introduced by
self::instance is always a sub-type of AOE\Crawler\Event\EventDispatcher.
Loading history...
160 16
            $dispatcher = new EventDispatcher();
0 ignored issues
show
Deprecated Code introduced by
The class AOE\Crawler\Event\EventDispatcher has been deprecated: Is deprecated since 9.0.1 and will be removed in v10.x ( Ignorable by Annotation )

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

160
            $dispatcher = /** @scrutinizer ignore-deprecated */ new EventDispatcher();
Loading history...
161 16
            self::$instance = $dispatcher;
162
        }
163
164 16
        return self::$instance;
165
    }
166
}
167