Passed
Pull Request — master (#80)
by Romain
21:44 queued 18:16
created

NotificationDefinition::getSettings()   A

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
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\Notification;
18
19
use CuyZ\Notiz\Core\Definition\Tree\AbstractDefinitionComponent;
20
use CuyZ\Notiz\Core\Definition\Tree\Notification\Channel\ChannelDefinition;
21
use CuyZ\Notiz\Core\Exception\ClassNotFoundException;
22
use CuyZ\Notiz\Core\Exception\InvalidClassException;
23
use CuyZ\Notiz\Core\Exception\NotizException;
24
use CuyZ\Notiz\Core\Notification\CustomSettingsNotification;
25
use CuyZ\Notiz\Core\Notification\Processor\NotificationProcessor;
26
use CuyZ\Notiz\Core\Notification\Processor\NotificationProcessorFactory;
27
use CuyZ\Notiz\Core\Notification\Settings\NotificationSettings;
28
use CuyZ\Notiz\Core\Support\NotizConstants;
29
use CuyZ\Notiz\Service\IconService;
30
use CuyZ\Notiz\Service\LocalizationService;
31
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
32
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
33
use TYPO3\CMS\Extbase\Error\Error;
34
35
class NotificationDefinition extends AbstractDefinitionComponent implements DataPreProcessorInterface
36
{
37
    const DEFAULT_ICON_PATH = NotizConstants::EXTENSION_ICON_DEFAULT;
38
39
    /**
40
     * @var string
41
     *
42
     * @validate NotEmpty
43
     */
44
    protected $identifier;
45
46
    /**
47
     * @var string
48
     */
49
    protected $label;
50
51
    /**
52
     * @var string
53
     */
54
    protected $description;
55
56
    /**
57
     * @var string
58
     *
59
     * @validate NotEmpty
60
     * @validate Romm.ConfigurationObject:ClassImplements(interface=CuyZ\Notiz\Core\Notification\Notification)
61
     */
62
    protected $className;
63
64
    /**
65
     * @var NotificationSettings
66
     *
67
     * @mixedTypesResolver \CuyZ\Notiz\Core\Definition\Tree\Notification\Settings\NotificationSettingsResolver
68
     */
69
    protected $settings;
70
71
    /**
72
     * @var \CuyZ\Notiz\Core\Definition\Tree\Notification\Channel\ChannelDefinition[]
73
     *
74
     * @validate NotEmpty
75
     */
76
    protected $channels = [];
77
78
    /**
79
     * @var string
80
     *
81
     * @validate Romm.ConfigurationObject:FileExists
82
     */
83
    protected $iconPath;
84
85
    /**
86
     * @param string $identifier
87
     */
88
    public function __construct($identifier)
89
    {
90
        $this->identifier = $identifier;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getIdentifier()
97
    {
98
        return $this->identifier;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getLabel()
105
    {
106
        return $this->label
107
            ? LocalizationService::localize($this->label)
108
            : $this->identifier;
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 NotificationSettings
129
     */
130
    public function getSettings()
131
    {
132
        return $this->settings;
133
    }
134
135
    /**
136
     * @return ChannelDefinition[]
137
     */
138
    public function getChannels()
139
    {
140
        return $this->channels;
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getIconPath()
147
    {
148
        return $this->iconPath ?: self::DEFAULT_ICON_PATH;
149
    }
150
151
    /**
152
     * The icon will be registered in the TYPO3 icon registry, using the icon
153
     * path.
154
     *
155
     * @return string
156
     */
157
    public function getIconIdentifier()
158
    {
159
        return IconService::get()->registerNotificationIcon($this);
160
    }
161
162
    /**
163
     * @return NotificationProcessor
164
     */
165
    public function getProcessor()
166
    {
167
        return NotificationProcessorFactory::get()->getFromNotificationClassName($this->getClassName());
168
    }
169
170
    /**
171
     * Method called during the definition object construction: it allows
172
     * manipulating the data array before it is actually used to construct the
173
     * object.
174
     *
175
     * We use it to:
176
     *
177
     * - Automatically fill the `identifier` property of the channels with the
178
     *   keys of the array.
179
     * - Add the settings class name further in the data array so it can be
180
     *   fetched later.
181
     *
182
     * @param DataPreProcessor $processor
183
     */
184
    public static function dataPreProcessor(DataPreProcessor $processor)
185
    {
186
        self::forceIdentifierForProperty($processor, 'channels');
187
188
        $data = $processor->getData();
189
190
        // Settings must always be set.
191
        if (!is_array($data['settings'])) {
192
            $data['settings'] = [];
193
        }
194
195
        $data['settings'][NotificationSettings::SETTINGS_CLASS_NAME] = NotificationSettings::TYPE_DEFAULT;
196
197
        try {
198
            $data = self::fetchSettingsClassName($data);
199
        } catch (NotizException $exception) {
200
            $error = new Error($exception->getMessage(), $exception->getCode());
201
            $processor->addError($error);
202
        }
203
204
        $processor->setData($data);
205
    }
206
207
    /**
208
     * @param array $data
209
     * @return array
210
     *
211
     * @throws ClassNotFoundException
212
     * @throws InvalidClassException
213
     */
214
    protected static function fetchSettingsClassName(array $data)
215
    {
216
        // @PHP7
217
        $notificationClassName = isset($data['className'])
218
            ? $data['className']
219
            : null;
220
221
        if (class_exists($notificationClassName)
222
            && in_array(CustomSettingsNotification::class, class_implements($notificationClassName))
223
        ) {
224
            /** @var CustomSettingsNotification $notificationClassName */
225
            $settingsClassName = $notificationClassName::getSettingsClassName();
226
227
            if (!class_exists($settingsClassName)) {
228
                throw ClassNotFoundException::notificationSettingsClassNotFound($settingsClassName);
229
            }
230
231
            if (!in_array(NotificationSettings::class, class_implements($settingsClassName))) {
232
                throw InvalidClassException::notificationSettingsMissingInterface($settingsClassName);
233
            }
234
235
            $data['settings'][NotificationSettings::SETTINGS_CLASS_NAME] = $settingsClassName;
236
        }
237
238
        return $data;
239
    }
240
}
241