Definition::hasNotification()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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\Definition\Tree;
19
20
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
21
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\EventGroup;
22
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
23
use CuyZ\Notiz\Core\Exception\EntryNotFoundException;
24
use CuyZ\Notiz\Core\Support\NotizConstants;
25
use Generator;
26
use Romm\ConfigurationObject\ConfigurationObjectInterface;
27
use Romm\ConfigurationObject\Service\Items\Cache\CacheService;
28
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
29
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
30
use Romm\ConfigurationObject\Service\ServiceFactory;
31
use Romm\ConfigurationObject\Service\ServiceInterface;
32
use Romm\ConfigurationObject\Traits\ConfigurationObject\ArrayConversionTrait;
33
34
/**
35
 * Root object for the whole definition object tree.
36
 *
37
 * From this very object you can access to any definition value that you need.
38
 */
39
class Definition extends AbstractDefinitionComponent implements ConfigurationObjectInterface, DataPreProcessorInterface
40
{
41
    use ArrayConversionTrait;
42
43
    /**
44
     * @var \CuyZ\Notiz\Core\Definition\Tree\EventGroup\EventGroup[]
45
     *
46
     * @validate NotEmpty
47
     */
48
    protected $eventGroups = [];
49
50
    /**
51
     * @var \CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition[]
52
     */
53
    protected $notifications = [];
54
55
    /**
56
     * @return EventGroup[]
57
     */
58
    public function getEventGroups(): array
59
    {
60
        return $this->eventGroups;
61
    }
62
63
    /**
64
     * @return Generator|EventDefinition[]
65
     */
66
    public function getEvents(): Generator
67
    {
68
        foreach ($this->eventGroups as $eventGroup) {
69
            foreach ($eventGroup->getEvents() as $event) {
70
                yield $eventGroup => $event;
71
            }
72
        }
73
    }
74
75
    /**
76
     * @param string $identifier
77
     * @return bool
78
     */
79
    public function hasEventGroup(string $identifier): bool
80
    {
81
        return true === isset($this->eventGroups[$identifier]);
82
    }
83
84
    /**
85
     * @param string $identifier
86
     * @return EventGroup
87
     *
88
     * @throws EntryNotFoundException
89
     */
90
    public function getEventGroup(string $identifier): EventGroup
91
    {
92
        if (false === $this->hasEventGroup($identifier)) {
93
            throw EntryNotFoundException::definitionEventGroupNotFound($identifier);
94
        }
95
96
        return $this->eventGroups[$identifier];
97
    }
98
99
    /**
100
     * @param string $fullIdentifier
101
     * @return EventDefinition
102
     *
103
     * @throws EntryNotFoundException
104
     */
105
    public function getEventFromFullIdentifier(string $fullIdentifier): EventDefinition
106
    {
107
        if (!$this->hasEventFromFullIdentifier($fullIdentifier)) {
108
            throw EntryNotFoundException::definitionEventFullIdentifierNotFound($fullIdentifier);
109
        }
110
111
        list($eventGroup, $event) = explode('.', $fullIdentifier);
112
113
        return $this->getEventGroup($eventGroup)->getEvent($event);
114
    }
115
116
    /**
117
     * @param string $fullIdentifier
118
     * @return bool
119
     */
120
    public function hasEventFromFullIdentifier(string $fullIdentifier): bool
121
    {
122
        list($eventGroup, $event) = explode('.', $fullIdentifier);
123
124
        return $this->hasEventGroup($eventGroup)
125
            && $this->getEventGroup($eventGroup)->hasEvent($event);
126
    }
127
128
    /**
129
     * @return EventGroup
130
     */
131
    public function getFirstEventGroup(): EventGroup
132
    {
133
        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

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