NotificationTcaService::appendOptionGroup()   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 2
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\Notification\Service;
19
20
use CuyZ\Notiz\Core\Definition\DefinitionService;
21
use CuyZ\Notiz\Core\Definition\Tree\Definition;
22
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
23
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
24
use CuyZ\Notiz\Core\Event\Service\EventFactory;
25
use CuyZ\Notiz\Core\Exception\NotImplementedException;
26
use CuyZ\Notiz\Core\Notification\Notification;
27
use CuyZ\Notiz\Core\Support\NotizConstants;
28
use CuyZ\Notiz\Domain\Property\Marker;
29
use CuyZ\Notiz\Service\Container;
30
use CuyZ\Notiz\Service\LocalizationService;
31
use CuyZ\Notiz\Service\StringService;
32
use CuyZ\Notiz\Service\ViewService;
33
use TYPO3\CMS\Core\SingletonInterface;
34
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Persis...neric\Mapper\DataMapper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
36
/**
37
 * Utility service to ease TCA manipulation for TYPO3 notification records.
38
 */
39
abstract class NotificationTcaService implements SingletonInterface
40
{
41
    /**
42
     * @var EventFactory
43
     */
44
    protected $eventFactory;
45
46
    /**
47
     * @var DefinitionService
48
     */
49
    protected $definitionService;
50
51
    /**
52
     * @var ViewService
53
     */
54
    protected $viewService;
55
56
    /**
57
     * @var DataMapper
58
     */
59
    protected $dataMapper;
60
61
    /**
62
     * @var Notification[]
63
     */
64
    private $notification;
65
66
    /**
67
     * Manual dependency injection.
68
     */
69
    public function __construct()
70
    {
71
        $this->eventFactory = Container::get(EventFactory::class);
72
        $this->definitionService = Container::get(DefinitionService::class);
73
        $this->viewService = Container::get(ViewService::class);
74
        $this->dataMapper = Container::get(DataMapper::class);
75
    }
76
77
    /**
78
     * Loads all available events and stores them as an array to be used in the
79
     * TCA.
80
     *
81
     * @param array $parameters
82
     */
83
    public function getEventsList(array &$parameters)
84
    {
85
        if ($this->definitionHasErrors()) {
86
            return;
87
        }
88
89
        $eventGroups = $this->getDefinition()->getEventGroups();
90
91
        foreach ($eventGroups as $eventGroup) {
92
            $parameters['items'][] = [
93
                $eventGroup->getLabel(),
94
                '--div--',
95
            ];
96
97
            foreach ($eventGroup->getEvents() as $event) {
98
                $parameters['items'][] = [
99
                    $event->getLabel(),
100
                    $event->getFullIdentifier(),
101
                ];
102
            }
103
        }
104
    }
105
106
    /**
107
     * This function will fetch the event selected in the given notification.
108
     *
109
     * If the notification is new or if the event identifier is not found, the
110
     * first event from the first event group is returned.
111
     *
112
     * @param array $row
113
     * @return EventDefinition
114
     */
115
    protected function getSelectedEvent(array $row): EventDefinition
116
    {
117
        $definition = $this->getDefinition();
118
119
        // The first configured event is selected by default.
120
        $event = $definition->getFirstEventGroup()->getFirstEvent();
121
122
        if (isset($row['event']) && !empty($row['event'])) {
123
            $eventValue = is_array($row['event'])
124
                ? $row['event'][0]
125
                : $row['event'];
126
127
            if ($definition->hasEventFromFullIdentifier($eventValue)) {
128
                $event = $definition->getEventFromFullIdentifier($eventValue);
129
            }
130
        }
131
132
        return $event;
133
    }
134
135
    /**
136
     * Loads all markers for the current selected event and formats them as a
137
     * list to be displayed on the edit form.
138
     *
139
     * @param array $parameters
140
     * @return string
141
     */
142
    public function getMarkersLabel(array &$parameters): string
143
    {
144
        if ($this->definitionHasErrors()) {
145
            return '';
146
        }
147
148
        $row = $parameters['row'];
149
        $eventDefinition = $this->getSelectedEvent($row);
150
        $notification = $this->getNotification($row);
151
152
        /** @var Marker[] $markers */
153
        $markers = $eventDefinition->getPropertyDefinition(Marker::class, $notification)->getEntries();
154
155
        $output = '';
156
157
        foreach ($markers as $marker) {
158
            $label = StringService::mark($marker->getLabel());
159
            $output .= "<tr><td><strong>{$marker->getFormattedName()}</strong></td><td>$label</td></tr>";
160
        }
161
162
        $description = LocalizationService::localize('Notification/Entity:field.markers.description', [$eventDefinition->getLabel()]);
163
164
        return <<<HTML
165
<p>$description</p>
166
167
<table class="table table-striped table-hover">
168
    <tbody>
169
        $output
170
    </tbody>
171
</table>
172
HTML;
173
    }
174
175
    /**
176
     * Loads all available channels and stores them as an array to be used in
177
     * the TCA.
178
     *
179
     * @param array $parameters
180
     */
181
    public function getChannelsList(array &$parameters)
182
    {
183
        if ($this->definitionHasErrors()) {
184
            return;
185
        }
186
187
        foreach ($this->getNotificationDefinition()->getChannels() as $channelDefinition) {
188
            $label = $channelDefinition->getLabel();
189
190
            if (empty($label)) {
191
                $label = $channelDefinition->getIdentifier();
192
            }
193
194
            $parameters['items'][] = [$label, $channelDefinition->getClassName()];
195
        }
196
    }
197
198
    /**
199
     * Returns a notification object based on an array containing the
200
     * notification properties.
201
     *
202
     * @param array $row
203
     * @return Notification
204
     */
205
    protected function getNotification(array $row): Notification
206
    {
207
        $hash = json_encode($row);
208
209
        if (!isset($this->notification[$hash])) {
210
            $this->notification[$hash] = isset($row['uid']) && is_integer($row['uid'])
211
                ? $this->getNotificationDefinition()->getProcessor()->getNotificationFromIdentifier((string)$row['uid'])
212
                : reset($this->dataMapper->map($this->getNotificationDefinition()->getClassName(), [$row]));
213
        }
214
215
        return $this->notification[$hash];
216
    }
217
218
    /**
219
     * @param array $array
220
     * @param $label
221
     */
222
    protected function appendOptionGroup(array &$array, $label)
223
    {
224
        array_unshift($array, ['label' => "––– $label –––", 'value' => '--div--']);
225
    }
226
227
    /**
228
     * @return string
229
     */
230
    public function getNotificationIconPath(): string
231
    {
232
        if ($this->definitionService->getValidationResult()->hasErrors()) {
233
            return NotizConstants::EXTENSION_ICON_DEFAULT;
234
        }
235
236
        return $this->getNotificationDefinition()->getIconPath();
237
    }
238
239
    /**
240
     * @return Definition
241
     */
242
    public function getDefinition(): Definition
243
    {
244
        return $this->definitionService->getDefinition();
245
    }
246
247
    /**
248
     * @return NotificationDefinition
249
     */
250
    protected function getNotificationDefinition(): NotificationDefinition
251
    {
252
        return $this->getDefinition()->getNotification($this->getDefinitionIdentifier());
253
    }
254
255
    /**
256
     * This method must return the current notification identifier to be used to
257
     * retrieve the current notification definition.
258
     *
259
     * @return string
260
     * @throws NotImplementedException
261
     */
262
    protected function getDefinitionIdentifier(): string
263
    {
264
        throw NotImplementedException::tcaServiceNotificationIdentifierMissing(__METHOD__);
265
    }
266
267
    /**
268
     * @return bool
269
     */
270
    public function definitionHasErrors(): bool
271
    {
272
        return $this->definitionService->getValidationResult()->hasErrors();
273
    }
274
}
275