EventGroup   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 98
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getLabel() 0 3 1
A dataPreProcessor() 0 3 1
A getFirstEvent() 0 3 1
A hasEvent() 0 3 1
A getEvent() 0 7 2
A getEvents() 0 3 1
A getIdentifier() 0 3 1
A __construct() 0 3 1
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\Definition\Tree\EventGroup;
19
20
use CuyZ\Notiz\Core\Definition\Tree\AbstractDefinitionComponent;
21
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
22
use CuyZ\Notiz\Core\Exception\EntryNotFoundException;
23
use CuyZ\Notiz\Service\LocalizationService;
24
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
25
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
26
27
class EventGroup extends AbstractDefinitionComponent implements DataPreProcessorInterface
28
{
29
    /**
30
     * @var string
31
     *
32
     * @validate NotEmpty
33
     */
34
    protected $identifier;
35
36
    /**
37
     * @var string
38
     */
39
    protected $label;
40
41
    /**
42
     * @var \CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition[]
43
     *
44
     * @validate NotEmpty
45
     */
46
    protected $events = [];
47
48
    /**
49
     * @param string $identifier
50
     */
51
    public function __construct(string $identifier)
52
    {
53
        $this->identifier = $identifier;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getIdentifier(): string
60
    {
61
        return $this->identifier;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getLabel(): string
68
    {
69
        return LocalizationService::localize($this->label);
70
    }
71
72
    /**
73
     * @return EventDefinition[]
74
     */
75
    public function getEvents(): array
76
    {
77
        return $this->events;
78
    }
79
80
    /**
81
     * @param string $identifier
82
     * @return bool
83
     */
84
    public function hasEvent(string $identifier): bool
85
    {
86
        return true === isset($this->events[$identifier]);
87
    }
88
89
    /**
90
     * @param string $identifier
91
     * @return EventDefinition
92
     *
93
     * @throws EntryNotFoundException
94
     */
95
    public function getEvent(string $identifier): EventDefinition
96
    {
97
        if (false === $this->hasEvent($identifier)) {
98
            throw EntryNotFoundException::definitionEventNotFound($identifier);
99
        }
100
101
        return $this->events[$identifier];
102
    }
103
104
    /**
105
     * @return EventDefinition
106
     */
107
    public function getFirstEvent(): EventDefinition
108
    {
109
        return array_pop(array_reverse($this->getEvents()));
0 ignored issues
show
Bug introduced by
array_reverse($this->getEvents()) cannot be passed to array_pop() as the parameter $array expects a reference. ( Ignorable by Annotation )

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

109
        return array_pop(/** @scrutinizer ignore-type */ array_reverse($this->getEvents()));
Loading history...
110
    }
111
112
    /**
113
     * Method called during the definition object construction: it allows
114
     * manipulating the data array before it is actually used to construct the
115
     * object.
116
     *
117
     * We use it to automatically fill the `identifier` property of the events
118
     * with the keys of the array.
119
     *
120
     * @param DataPreProcessor $processor
121
     */
122
    public static function dataPreProcessor(DataPreProcessor $processor)
123
    {
124
        self::forceIdentifierForProperty($processor, 'events');
125
    }
126
}
127