Passed
Push — master ( 4595aa...20e07b )
by Romain
06:34
created

NotificationDefinition::getProcessor()   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
     * @validate NotEmpty
55
     * @validate Romm.ConfigurationObject:ClassImplements(interface=CuyZ\Notiz\Core\Notification\Notification)
56
     */
57
    protected $className;
58
59
    /**
60
     * @var NotificationSettings
61
     *
62
     * @mixedTypesResolver \CuyZ\Notiz\Core\Definition\Tree\Notification\Settings\NotificationSettingsResolver
63
     */
64
    protected $settings;
65
66
    /**
67
     * @var \CuyZ\Notiz\Core\Definition\Tree\Notification\Channel\ChannelDefinition[]
68
     *
69
     * @validate NotEmpty
70
     */
71
    protected $channels = [];
72
73
    /**
74
     * @var string
75
     *
76
     * @validate Romm.ConfigurationObject:FileExists
77
     */
78
    protected $iconPath;
79
80
    /**
81
     * @param string $identifier
82
     */
83
    public function __construct($identifier)
84
    {
85
        $this->identifier = $identifier;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getIdentifier()
92
    {
93
        return $this->identifier;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getLabel()
100
    {
101
        return $this->label
102
            ? LocalizationService::localize($this->label)
103
            : $this->identifier;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getClassName()
110
    {
111
        return $this->className;
112
    }
113
114
    /**
115
     * @return NotificationSettings
116
     */
117
    public function getSettings()
118
    {
119
        return $this->settings;
120
    }
121
122
    /**
123
     * @return ChannelDefinition[]
124
     */
125
    public function getChannels()
126
    {
127
        return $this->channels;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getIconPath()
134
    {
135
        return $this->iconPath ?: self::DEFAULT_ICON_PATH;
136
    }
137
138
    /**
139
     * The icon will be registered in the TYPO3 icon registry, using the icon
140
     * path.
141
     *
142
     * @return string
143
     */
144
    public function getIconIdentifier()
145
    {
146
        return IconService::get()->registerNotificationIcon($this);
147
    }
148
149
    /**
150
     * @return NotificationProcessor
151
     */
152
    public function getProcessor()
153
    {
154
        return NotificationProcessorFactory::get()->getFromNotificationClassName($this->getClassName());
155
    }
156
157
    /**
158
     * Method called during the definition object construction: it allows
159
     * manipulating the data array before it is actually used to construct the
160
     * object.
161
     *
162
     * We use it to:
163
     *
164
     * - Automatically fill the `identifier` property of the channels with the
165
     *   keys of the array.
166
     * - Add the settings class name further in the data array so it can be
167
     *   fetched later.
168
     *
169
     * @param DataPreProcessor $processor
170
     */
171
    public static function dataPreProcessor(DataPreProcessor $processor)
172
    {
173
        self::forceIdentifierForProperty($processor, 'channels');
174
175
        $data = $processor->getData();
176
177
        // Settings must always be set.
178
        if (!is_array($data['settings'])) {
179
            $data['settings'] = [];
180
        }
181
182
        $data['settings'][NotificationSettings::SETTINGS_CLASS_NAME] = NotificationSettings::TYPE_DEFAULT;
183
184
        try {
185
            $data = self::fetchSettingsClassName($data);
186
        } catch (NotizException $exception) {
187
            $error = new Error($exception->getMessage(), $exception->getCode());
188
            $processor->addError($error);
189
        }
190
191
        $processor->setData($data);
192
    }
193
194
    /**
195
     * @param array $data
196
     * @return array
197
     *
198
     * @throws ClassNotFoundException
199
     * @throws InvalidClassException
200
     */
201
    protected static function fetchSettingsClassName(array $data)
202
    {
203
        // @PHP7
204
        $notificationClassName = isset($data['className'])
205
            ? $data['className']
206
            : null;
207
208
        if (class_exists($notificationClassName)
209
            && in_array(CustomSettingsNotification::class, class_implements($notificationClassName))
210
        ) {
211
            /** @var CustomSettingsNotification $notificationClassName */
212
            $settingsClassName = $notificationClassName::getSettingsClassName();
213
214
            if (!class_exists($settingsClassName)) {
215
                throw ClassNotFoundException::notificationSettingsClassNotFound($settingsClassName);
216
            }
217
218
            if (!in_array(NotificationSettings::class, class_implements($settingsClassName))) {
219
                throw InvalidClassException::notificationSettingsMissingInterface($settingsClassName);
220
            }
221
222
            $data['settings'][NotificationSettings::SETTINGS_CLASS_NAME] = $settingsClassName;
223
        }
224
225
        return $data;
226
    }
227
}
228