Passed
Branch master (265123)
by Romain
07:23 queued 03:20
created

Definition   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 156
rs 10
c 0
b 0
f 0
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getEventGroups() 0 3 1
A hasEventGroup() 0 3 1
A getEvents() 0 5 3
A getEventGroup() 0 7 2
A hasNotification() 0 3 1
A dataPreProcessor() 0 4 1
A getNotification() 0 7 2
A getNotifications() 0 3 1
A getConfigurationObjectServices() 0 8 1
A hasEventFromFullIdentifier() 0 6 2
A getEventFromFullIdentifier() 0 9 2
A getFirstEventGroup() 0 3 1
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Core\Definition\Tree;
18
19
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
20
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\EventGroup;
21
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
22
use CuyZ\Notiz\Core\Exception\EntryNotFoundException;
23
use CuyZ\Notiz\Core\Support\NotizConstants;
24
use Generator;
25
use Romm\ConfigurationObject\ConfigurationObjectInterface;
26
use Romm\ConfigurationObject\Service\Items\Cache\CacheService;
27
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
28
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
29
use Romm\ConfigurationObject\Service\ServiceFactory;
30
use Romm\ConfigurationObject\Service\ServiceInterface;
31
use Romm\ConfigurationObject\Traits\ConfigurationObject\ArrayConversionTrait;
32
33
/**
34
 * Root object for the whole definition object tree.
35
 *
36
 * From this very object you can access to any definition value that you need.
37
 */
38
class Definition extends AbstractDefinitionComponent implements ConfigurationObjectInterface, DataPreProcessorInterface
39
{
40
    use ArrayConversionTrait;
41
42
    /**
43
     * @var \CuyZ\Notiz\Core\Definition\Tree\EventGroup\EventGroup[]
44
     *
45
     * @validate NotEmpty
46
     */
47
    protected $eventGroups = [];
48
49
    /**
50
     * @var \CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition[]
51
     */
52
    protected $notifications = [];
53
54
    /**
55
     * @return EventGroup[]
56
     */
57
    public function getEventGroups()
58
    {
59
        return $this->eventGroups;
60
    }
61
62
    /**
63
     * @return Generator|EventDefinition[]
64
     */
65
    public function getEvents()
66
    {
67
        foreach ($this->eventGroups as $eventGroup) {
68
            foreach ($eventGroup->getEvents() as $event) {
69
                yield $eventGroup => $event;
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param string $identifier
76
     * @return bool
77
     */
78
    public function hasEventGroup($identifier)
79
    {
80
        return true === isset($this->eventGroups[$identifier]);
81
    }
82
83
    /**
84
     * @param string $identifier
85
     * @return EventGroup
86
     *
87
     * @throws EntryNotFoundException
88
     */
89
    public function getEventGroup($identifier)
90
    {
91
        if (false === $this->hasEventGroup($identifier)) {
92
            throw EntryNotFoundException::definitionEventGroupNotFound($identifier);
93
        }
94
95
        return $this->eventGroups[$identifier];
96
    }
97
98
    /**
99
     * @param string $fullIdentifier
100
     * @return EventDefinition
101
     *
102
     * @throws EntryNotFoundException
103
     */
104
    public function getEventFromFullIdentifier($fullIdentifier)
105
    {
106
        if (!$this->hasEventFromFullIdentifier($fullIdentifier)) {
107
            throw EntryNotFoundException::definitionEventFullIdentifierNotFound($fullIdentifier);
108
        }
109
110
        list($eventGroup, $event) = explode('.', $fullIdentifier);
111
112
        return $this->getEventGroup($eventGroup)->getEvent($event);
113
    }
114
115
    /**
116
     * @param string $fullIdentifier
117
     * @return bool
118
     */
119
    public function hasEventFromFullIdentifier($fullIdentifier)
120
    {
121
        list($eventGroup, $event) = explode('.', $fullIdentifier);
122
123
        return $this->hasEventGroup($eventGroup)
124
            && $this->getEventGroup($eventGroup)->hasEvent($event);
125
    }
126
127
    /**
128
     * @return EventGroup
129
     */
130
    public function getFirstEventGroup()
131
    {
132
        return array_pop(array_reverse($this->getEventGroups()));
0 ignored issues
show
Bug introduced by
array_reverse($this->getEventGroups()) 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

132
        return array_pop(/** @scrutinizer ignore-type */ array_reverse($this->getEventGroups()));
Loading history...
133
    }
134
135
    /**
136
     * @return NotificationDefinition[]
137
     */
138
    public function getNotifications()
139
    {
140
        return $this->notifications;
141
    }
142
143
    /**
144
     * @param $identifier
145
     * @return bool
146
     */
147
    public function hasNotification($identifier)
148
    {
149
        return true === isset($this->notifications[$identifier]);
150
    }
151
152
    /**
153
     * @param string $identifier
154
     * @return NotificationDefinition
155
     *
156
     * @throws EntryNotFoundException
157
     */
158
    public function getNotification($identifier)
159
    {
160
        if (false === $this->hasNotification($identifier)) {
161
            throw EntryNotFoundException::definitionNotificationNotFound($identifier);
162
        }
163
164
        return $this->notifications[$identifier];
165
    }
166
167
    /**
168
     * @inheritdoc
169
     */
170
    public static function getConfigurationObjectServices()
171
    {
172
        return ServiceFactory::getInstance()
173
            ->attach(ServiceInterface::SERVICE_CACHE)
174
            ->setOption(CacheService::OPTION_CACHE_NAME, NotizConstants::CACHE_KEY_DEFINITION_OBJECT)
175
            ->attach(ServiceInterface::SERVICE_PARENTS)
176
            ->attach(ServiceInterface::SERVICE_DATA_PRE_PROCESSOR)
177
            ->attach(ServiceInterface::SERVICE_MIXED_TYPES);
178
    }
179
180
    /**
181
     * Method called during the definition object construction: it allows
182
     * manipulating the data array before it is actually used to construct the
183
     * object.
184
     *
185
     * We use it to automatically fill the `identifier` property of the event
186
     * groups and notifications with the keys of the array.
187
     *
188
     * @param DataPreProcessor $processor
189
     */
190
    public static function dataPreProcessor(DataPreProcessor $processor)
191
    {
192
        self::forceIdentifierForProperty($processor, 'eventGroups');
193
        self::forceIdentifierForProperty($processor, 'notifications');
194
    }
195
}
196