Passed
Pull Request — master (#47)
by Romain
03:39
created

Definition::getEvents()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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
     * @return EventGroup
100
     */
101
    public function getFirstEventGroup()
102
    {
103
        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

103
        return array_pop(/** @scrutinizer ignore-type */ array_reverse($this->getEventGroups()));
Loading history...
104
    }
105
106
    /**
107
     * @return NotificationDefinition[]
108
     */
109
    public function getNotifications()
110
    {
111
        return $this->notifications;
112
    }
113
114
    /**
115
     * @param $identifier
116
     * @return bool
117
     */
118
    public function hasNotification($identifier)
119
    {
120
        return true === isset($this->notifications[$identifier]);
121
    }
122
123
    /**
124
     * @param string $identifier
125
     * @return NotificationDefinition
126
     *
127
     * @throws EntryNotFoundException
128
     */
129
    public function getNotification($identifier)
130
    {
131
        if (false === $this->hasNotification($identifier)) {
132
            throw EntryNotFoundException::definitionNotificationNotFound($identifier);
133
        }
134
135
        return $this->notifications[$identifier];
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141
    public static function getConfigurationObjectServices()
142
    {
143
        return ServiceFactory::getInstance()
144
            ->attach(ServiceInterface::SERVICE_CACHE)
145
            ->setOption(CacheService::OPTION_CACHE_NAME, NotizConstants::CACHE_KEY_DEFINITION_OBJECT)
146
            ->attach(ServiceInterface::SERVICE_PARENTS)
147
            ->attach(ServiceInterface::SERVICE_DATA_PRE_PROCESSOR)
148
            ->attach(ServiceInterface::SERVICE_MIXED_TYPES);
149
    }
150
151
    /**
152
     * Method called during the definition object construction: it allows
153
     * manipulating the data array before it is actually used to construct the
154
     * object.
155
     *
156
     * We use it to automatically fill the `identifier` property of the event
157
     * groups and notifications with the keys of the array.
158
     *
159
     * @param DataPreProcessor $processor
160
     */
161
    public static function dataPreProcessor(DataPreProcessor $processor)
162
    {
163
        self::forceIdentifierForProperty($processor, 'eventGroups');
164
        self::forceIdentifierForProperty($processor, 'notifications');
165
    }
166
}
167