EventRegistry::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Core\Event\Service;
19
20
use CuyZ\Notiz\Core\Definition\DefinitionService;
21
use CuyZ\Notiz\Core\Event\Runner\EventRunnerContainer;
22
use CuyZ\Notiz\Service\Traits\ExtendedSelfInstantiateTrait;
23
use TYPO3\CMS\Core\SingletonInterface;
24
25
/**
26
 * This class will do all the registration work for every event that was added
27
 * to the definition.
28
 *
29
 * Events are bound to signals/hooks, this class will process every event and
30
 * call their own registration method.
31
 */
32
class EventRegistry implements SingletonInterface
33
{
34
    use ExtendedSelfInstantiateTrait;
35
36
    /**
37
     * @var bool
38
     */
39
    protected $registrationDone = false;
40
41
    /**
42
     * @var DefinitionService
43
     */
44
    protected $definitionService;
45
46
    /**
47
     * @var EventRunnerContainer
48
     */
49
    protected $eventRunnerContainer;
50
51
    /**
52
     * @param DefinitionService $definitionService
53
     * @param EventRunnerContainer $eventRunnerContainer
54
     */
55
    public function __construct(DefinitionService $definitionService, EventRunnerContainer $eventRunnerContainer)
56
    {
57
        $this->definitionService = $definitionService;
58
        $this->eventRunnerContainer = $eventRunnerContainer;
59
    }
60
61
    /**
62
     * If an error is found in the definition validation result, the events
63
     * registration is aborted.
64
     *
65
     * @internal
66
     */
67
    public function registerEvents()
68
    {
69
        if ($this->registrationDone) {
70
            return;
71
        }
72
73
        $this->registrationDone = true;
74
75
        if (!$this->definitionService->getValidationResult()->hasErrors()) {
76
            $this->registerEventsInternal();
77
        }
78
    }
79
80
    /**
81
     * Loops on each event entry in the definition, and calls the `register()`
82
     * method that will do the actual registration work of the signals/hooks.
83
     */
84
    protected function registerEventsInternal()
85
    {
86
        $definition = $this->definitionService->getDefinition();
87
88
        foreach ($definition->getEvents() as $eventDefinition) {
89
            $eventRunner = $this->eventRunnerContainer->add($eventDefinition);
90
91
            $eventDefinition->getConnection()->register($eventRunner);
92
        }
93
    }
94
}
95