Passed
Push — master ( bee4ae...fc5262 )
by Andreas
16:28
created

dispatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 39
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A trigger_watch() 0 5 1
A add_watches() 0 9 4
1
<?php
2
/**
3
 * @package midcom.events
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\events;
10
11
use Symfony\Component\EventDispatcher\EventDispatcher;
12
13
/**
14
 * midcom event dispatcher
15
 *
16
 * @package midcom.events
17
 */
18
class dispatcher extends EventDispatcher
19
{
20
    /**
21
     * This array contains all registered MidCOM operation watches. They are indexed by
22
     * operation and map to components / libraries which have registered to classes.
23
     * Values consist of an array whose first element is the component and subsequent
24
     * elements are the types involved (so a single count means all objects).
25
     *
26
     * @var array
27
     */
28
    private $watches = [
29
        \MIDCOM_OPERATION_DBA_CREATE => dbaevent::CREATE,
30
        \MIDCOM_OPERATION_DBA_UPDATE => dbaevent::UPDATE,
31
        \MIDCOM_OPERATION_DBA_DELETE => dbaevent::DELETE,
32
        \MIDCOM_OPERATION_DBA_IMPORT => dbaevent::IMPORT,
33
    ];
34
35
    /**
36
     * Compat function for ragnaroek-style events.
37
     *
38
     * @param int $operation_id One of the MIDCOM_OPERATION_DBA_ constants
39
     * @param \midcom_core_dbaobject $object The current object
40
     */
41
    public function trigger_watch($operation_id, $object)
42
    {
43
        $event_name = $this->watches[$operation_id];
44
        $event = new dbaevent($object);
45
        $this->dispatch($event_name, $event);
0 ignored issues
show
Bug introduced by
$event of type midcom\events\dbaevent is incompatible with the type null|string expected by parameter $eventName of Symfony\Component\EventD...tDispatcher::dispatch(). ( Ignorable by Annotation )

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

45
        $this->dispatch($event_name, /** @scrutinizer ignore-type */ $event);
Loading history...
46
    }
47
48
    public function add_watches(array $watches, $component)
49
    {
50
        foreach ($watches as $watch) {
51
            foreach ($this->watches as $operation_id => $event_name) {
52
                // Check whether the operations flag list from the component
53
                // contains the operation_id we're checking a watch for.
54
                if ($watch['operations'] & $operation_id) {
55
                    $listener = new watcher($component, $watch['classes']);
56
                    $this->addListener($event_name, [$listener, 'handle_event']);
57
                }
58
            }
59
        }
60
    }
61
}
62