EventDefinition::getIdentifier()   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 0
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\EventGroup\Event;
19
20
use CuyZ\Notiz\Core\Definition\Tree\AbstractDefinitionComponent;
21
use CuyZ\Notiz\Core\Definition\Tree\Definition;
22
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\Configuration\EventConfiguration;
23
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\Connection\Connection;
24
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\EventGroup;
25
use CuyZ\Notiz\Core\Notification\Notification;
26
use CuyZ\Notiz\Core\Property\Factory\PropertyDefinition;
27
use CuyZ\Notiz\Core\Property\Factory\PropertyFactory;
28
use CuyZ\Notiz\Service\LocalizationService;
29
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
30
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
31
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
32
33
class EventDefinition extends AbstractDefinitionComponent implements DataPreProcessorInterface
34
{
35
    use ParentsTrait;
36
37
    /**
38
     * @var string
39
     *
40
     * @validate NotEmpty
41
     */
42
    protected $identifier;
43
44
    /**
45
     * @var string
46
     */
47
    protected $label;
48
49
    /**
50
     * @var string
51
     */
52
    protected $description;
53
54
    /**
55
     * @var string
56
     *
57
     * @validate Romm.ConfigurationObject:ClassImplements(interface=CuyZ\Notiz\Core\Event\Event)
58
     */
59
    protected $className;
60
61
    /**
62
     * @var \CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\Configuration\EventConfiguration
63
     */
64
    protected $configuration;
65
66
    /**
67
     * @var \CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\Connection\Connection
68
     *
69
     * @validate NotEmpty
70
     *
71
     * @mixedTypesResolver \CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\Connection\ConnectionResolver
72
     */
73
    protected $connection;
74
75
    /**
76
     * @param string $identifier
77
     * @param string $className
78
     */
79
    public function __construct(string $identifier, string $className)
80
    {
81
        $this->identifier = $identifier;
82
        $this->className = $className;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getIdentifier(): string
89
    {
90
        return $this->identifier;
91
    }
92
93
    /**
94
     * Returns a full version of the identifier, containing both the event group
95
     * identifier and the event identifier, separated by a dot.
96
     *
97
     * @return string
98
     */
99
    public function getFullIdentifier(): string
100
    {
101
        return $this->getGroup()->getIdentifier() . '.' . $this->identifier;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getLabel(): string
108
    {
109
        return LocalizationService::localize($this->label);
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getDescription(): string
116
    {
117
        return LocalizationService::localize($this->description);
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getClassName(): string
124
    {
125
        return $this->className;
126
    }
127
128
    /**
129
     * @return EventConfiguration
130
     */
131
    public function getConfiguration(): EventConfiguration
132
    {
133
        return $this->configuration;
134
    }
135
136
    /**
137
     * @return Connection
138
     */
139
    public function getConnection(): Connection
140
    {
141
        return $this->connection;
142
    }
143
144
    /**
145
     * @return EventGroup
146
     */
147
    public function getGroup(): EventGroup
148
    {
149
        /** @var EventGroup $eventGroup */
150
        $eventGroup = $this->getFirstParent(EventGroup::class);
151
152
        return $eventGroup;
153
    }
154
155
    /**
156
     * @param string $propertyClassName
157
     * @param Notification $notification
158
     * @return PropertyDefinition
159
     */
160
    public function getPropertyDefinition(string $propertyClassName, Notification $notification): PropertyDefinition
161
    {
162
        return PropertyFactory::get()->getPropertyDefinition($propertyClassName, $this, $notification);
0 ignored issues
show
Bug introduced by
It seems like getPropertyDefinition() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

162
        return PropertyFactory::get()->/** @scrutinizer ignore-call */ getPropertyDefinition($propertyClassName, $this, $notification);
Loading history...
163
    }
164
165
    /**
166
     * Counts the number of notifications that are using this event.
167
     *
168
     * @return int
169
     */
170
    public function getNotificationNumber(): int
171
    {
172
        $counter = 0;
173
174
        /** @var Definition $definition */
175
        $definition = $this->getFirstParent(Definition::class);
176
177
        foreach ($definition->getListableNotifications() as $notification) {
178
            $counter += $notification->getProcessor()->countNotificationsFromEventDefinition($this);
179
        }
180
181
        return $counter;
182
    }
183
184
    /**
185
     * @param DataPreProcessor $processor
186
     */
187
    public static function dataPreProcessor(DataPreProcessor $processor)
188
    {
189
        $data = $processor->getData();
190
191
        // Configuration must always be set.
192
        if (!is_array($data['configuration'])) {
193
            $data['configuration'] = [];
194
        }
195
196
        $processor->setData($data);
197
    }
198
}
199