Passed
Push — master ( af3c37...5256b8 )
by Romain
08:34 queued 04:39
created

EventDefinition::getNotificationNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 12
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\EventGroup\Event;
18
19
use CuyZ\Notiz\Core\Definition\Tree\AbstractDefinitionComponent;
20
use CuyZ\Notiz\Core\Definition\Tree\Definition;
21
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\Configuration\EventConfiguration;
22
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\Connection\Connection;
23
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\EventGroup;
24
use CuyZ\Notiz\Core\Notification\Notification;
25
use CuyZ\Notiz\Core\Property\Factory\PropertyDefinition;
26
use CuyZ\Notiz\Core\Property\Factory\PropertyFactory;
27
use CuyZ\Notiz\Service\LocalizationService;
28
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
29
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
30
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
31
32
class EventDefinition extends AbstractDefinitionComponent implements DataPreProcessorInterface
33
{
34
    use ParentsTrait;
35
36
    /**
37
     * @var string
38
     *
39
     * @validate NotEmpty
40
     */
41
    protected $identifier;
42
43
    /**
44
     * @var string
45
     */
46
    protected $label;
47
48
    /**
49
     * @var string
50
     */
51
    protected $description;
52
53
    /**
54
     * @var string
55
     *
56
     * @validate Romm.ConfigurationObject:ClassImplements(interface=CuyZ\Notiz\Core\Event\Event)
57
     */
58
    protected $className;
59
60
    /**
61
     * @var \CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\Configuration\EventConfiguration
62
     */
63
    protected $configuration;
64
65
    /**
66
     * @var \CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\Connection\Connection
67
     *
68
     * @validate NotEmpty
69
     *
70
     * @mixedTypesResolver \CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\Connection\ConnectionResolver
71
     */
72
    protected $connection;
73
74
    /**
75
     * @param string $identifier
76
     * @param string $className
77
     */
78
    public function __construct($identifier, $className)
79
    {
80
        $this->identifier = $identifier;
81
        $this->className = $className;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getIdentifier()
88
    {
89
        return $this->identifier;
90
    }
91
92
    /**
93
     * Returns a full version of the identifier, containing both the event group
94
     * identifier and the event identifier, separated by a dot.
95
     *
96
     * @return string
97
     */
98
    public function getFullIdentifier()
99
    {
100
        return $this->getGroup()->getIdentifier() . '.' . $this->identifier;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getLabel()
107
    {
108
        return LocalizationService::localize($this->label);
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getDescription()
115
    {
116
        return LocalizationService::localize($this->description);
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getClassName()
123
    {
124
        return $this->className;
125
    }
126
127
    /**
128
     * @return EventConfiguration
129
     */
130
    public function getConfiguration()
131
    {
132
        return $this->configuration;
133
    }
134
135
    /**
136
     * @return Connection
137
     */
138
    public function getConnection()
139
    {
140
        return $this->connection;
141
    }
142
143
    /**
144
     * @return EventGroup
145
     */
146
    public function getGroup()
147
    {
148
        /** @var EventGroup $eventGroup */
149
        $eventGroup = $this->getFirstParent(EventGroup::class);
150
151
        return $eventGroup;
152
    }
153
154
    /**
155
     * @param string $propertyClassName
156
     * @param Notification $notification
157
     * @return PropertyDefinition
158
     */
159
    public function getPropertyDefinition($propertyClassName, Notification $notification)
160
    {
161
        return PropertyFactory::get()->getPropertyDefinition($propertyClassName, $this, $notification);
162
    }
163
164
    /**
165
     * Counts the number of notifications that are using this event.
166
     *
167
     * @return int
168
     */
169
    public function getNotificationNumber()
170
    {
171
        $counter = 0;
172
173
        /** @var Definition $definition */
174
        $definition = $this->getFirstParent(Definition::class);
175
176
        foreach ($definition->getNotifications() as $notification) {
177
            $counter += $notification->getProcessor()->countNotificationsFromEventDefinition($this);
178
        }
179
180
        return $counter;
181
    }
182
183
    /**
184
     * @param DataPreProcessor $processor
185
     */
186
    public static function dataPreProcessor(DataPreProcessor $processor)
187
    {
188
        $data = $processor->getData();
189
190
        // Configuration must always be set.
191
        if (!is_array($data['configuration'])) {
192
            $data['configuration'] = [];
193
        }
194
195
        $processor->setData($data);
196
    }
197
}
198