EventRunnerContainer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 12 2
A has() 0 3 1
A get() 0 7 2
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\Runner;
19
20
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
21
use CuyZ\Notiz\Core\Exception\EntryNotFoundException;
22
use CuyZ\Notiz\Service\Container;
23
use CuyZ\Notiz\Service\Traits\SelfInstantiateTrait;
24
use TYPO3\CMS\Core\SingletonInterface;
25
26
/**
27
 * Contains all the event runners.
28
 *
29
 * This class is mostly needed because of the way event hooks can be registered:
30
 *
31
 * @see \CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\Connection\Hook::preventEvalNeverIdealStuff
32
 */
33
class EventRunnerContainer implements SingletonInterface
34
{
35
    use SelfInstantiateTrait {
36
        get as getInstance;
37
    }
38
39
    /**
40
     * @var EventRunner[]
41
     */
42
    protected $entries;
43
44
    /**
45
     * @param EventDefinition $eventDefinition
46
     * @return EventRunner
47
     */
48
    public function add(EventDefinition $eventDefinition): EventRunner
49
    {
50
        $identifier = $eventDefinition->getFullIdentifier();
51
52
        if (false === $this->has($identifier)) {
53
            /** @var EventRunner $runner */
54
            $runner = Container::get(EventRunner::class, $eventDefinition);
55
56
            $this->entries[$identifier] = $runner;
57
        }
58
59
        return $this->entries[$identifier];
60
    }
61
62
    /**
63
     * @param string $identifier
64
     * @return bool
65
     */
66
    public function has(string $identifier): bool
67
    {
68
        return isset($this->entries[$identifier]);
69
    }
70
71
    /**
72
     * @param string $identifier
73
     * @return EventRunner
74
     *
75
     * @throws EntryNotFoundException
76
     */
77
    public function get(string $identifier): EventRunner
78
    {
79
        if (false === $this->has($identifier)) {
80
            throw EntryNotFoundException::eventRunnerEntryNotFound($identifier);
81
        }
82
83
        return $this->entries[$identifier];
84
    }
85
}
86