EntityNotification   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 369
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 91
c 7
b 0
f 0
dl 0
loc 369
rs 9.52
wmc 36

27 Methods

Rating   Name   Duplication   Size   Complexity  
A getEvent() 0 3 1
A getBackendUser() 0 3 1
A setDescription() 0 3 1
A setEvent() 0 3 1
A getDescription() 0 3 1
A setActive() 0 3 1
A getTitle() 0 3 1
A isCreatable() 0 4 2
A isActive() 0 3 1
A getNotificationDefinition() 0 3 1
A setTitle() 0 3 1
A getCreationUri() 0 20 2
A getChannel() 0 3 1
A getEventDefinition() 0 3 1
A setEventConfigurationFlex() 0 3 1
A hasEventDefinition() 0 3 1
A setChannel() 0 3 1
A getEventConfiguration() 0 10 2
A getSwitchActivationUri() 0 20 2
A isViewable() 0 3 1
A getTableName() 0 11 1
A isEditable() 0 24 5
A getEditionUri() 0 14 1
A getViewUri() 0 15 1
A shouldDispatch() 0 3 1
A isListable() 0 4 2
A getDefinition() 0 3 1
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\Domain\Notification;
19
20
use CuyZ\Notiz\Backend\Module\ManagerModuleHandler;
21
use CuyZ\Notiz\Core\Definition\DefinitionService;
22
use CuyZ\Notiz\Core\Definition\Tree\Definition;
23
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
24
use CuyZ\Notiz\Core\Definition\Tree\Notification\Channel\ChannelDefinition;
25
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
26
use CuyZ\Notiz\Core\Notification\MultipleChannelsNotification;
27
use CuyZ\Notiz\Core\Notification\Notification;
28
use CuyZ\Notiz\Service\Container;
29
use TYPO3\CMS\Backend\Routing\UriBuilder;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Backend\Routing\UriBuilder 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...
30
use TYPO3\CMS\Core\Type\Bitmask\Permission;
31
use TYPO3\CMS\Core\Utility\ArrayUtility;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Config...urationManagerInterface 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...
34
use TYPO3\CMS\Extbase\Domain\Model\BackendUser;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Domain\Model\BackendUser 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
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\DomainObject\AbstractEntity 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...
36
use TYPO3\CMS\Extbase\Service\FlexFormService;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Service\FlexFormService 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...
37
38
abstract class EntityNotification extends AbstractEntity implements Notification, MultipleChannelsNotification
39
{
40
    /**
41
     * @var string
42
     */
43
    protected $title;
44
45
    /**
46
     * @var string
47
     */
48
    protected $description;
49
50
    /**
51
     * @var string
52
     */
53
    protected $event;
54
55
    /**
56
     * @var string
57
     */
58
    protected $channel;
59
60
    /**
61
     * @var string
62
     */
63
    protected $eventConfigurationFlex;
64
65
    /**
66
     * @var array
67
     */
68
    protected $eventConfiguration;
69
70
    /**
71
     * @var \TYPO3\CMS\Extbase\Domain\Model\BackendUser
72
     * @lazy
73
     */
74
    protected $backendUser;
75
76
    /**
77
     * @var bool
78
     */
79
    protected $hidden;
80
81
    /**
82
     * @return string|null
83
     */
84
    public function getTitle()
85
    {
86
        return $this->title;
87
    }
88
89
    /**
90
     * @param string $title
91
     */
92
    public function setTitle(string $title)
93
    {
94
        $this->title = $title;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getDescription(): string
101
    {
102
        return $this->description;
103
    }
104
105
    /**
106
     * @param string $description
107
     */
108
    public function setDescription(string $description)
109
    {
110
        $this->description = $description;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getEvent(): string
117
    {
118
        return $this->event;
119
    }
120
121
    /**
122
     * @param string $event
123
     */
124
    public function setEvent(string $event)
125
    {
126
        $this->event = $event;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getChannel(): string
133
    {
134
        return $this->channel;
135
    }
136
137
    /**
138
     * @param string $channel
139
     */
140
    public function setChannel(string $channel)
141
    {
142
        $this->channel = $channel;
143
    }
144
145
    /**
146
     * @param string $eventConfigurationFlex
147
     */
148
    public function setEventConfigurationFlex(string $eventConfigurationFlex)
149
    {
150
        $this->eventConfigurationFlex = $eventConfigurationFlex;
151
    }
152
153
    /**
154
     * @return bool
155
     */
156
    public function isActive(): bool
157
    {
158
        return !$this->hidden;
159
    }
160
161
    /**
162
     * @param bool $active
163
     * @return void
164
     */
165
    public function setActive(bool $active)
166
    {
167
        $this->hidden = !$active;
168
    }
169
170
    /**
171
     * @param EventDefinition|null $eventDefinition [PHP 7.1]
172
     * @return string
173
     */
174
    public function getSwitchActivationUri(EventDefinition $eventDefinition = null): string
175
    {
176
        $arguments = [
177
            'notificationType' => $this->getNotificationDefinition()->getIdentifier(),
178
            'notificationIdentifier' => $this->getUid(),
179
        ];
180
181
        if ($eventDefinition) {
182
            $arguments['filterEvent'] = $eventDefinition->getFullIdentifier();
183
        }
184
185
        /** @var ManagerModuleHandler $managerModuleHandler */
186
        $managerModuleHandler = Container::get(ManagerModuleHandler::class);
187
188
        return (string)$managerModuleHandler
189
            ->getUriBuilder()
190
            ->forController('Backend\\Manager\\NotificationActivation')
191
            ->forAction('process')
192
            ->withArguments($arguments)
193
            ->build();
194
    }
195
196
    /**
197
     * @return NotificationDefinition
198
     */
199
    public function getNotificationDefinition(): NotificationDefinition
200
    {
201
        return self::getDefinition()->getNotification(static::getDefinitionIdentifier());
202
    }
203
204
    /**
205
     * @return bool
206
     */
207
    public function hasEventDefinition(): bool
208
    {
209
        return self::getDefinition()->hasEventFromFullIdentifier($this->getEvent());
210
    }
211
212
    /**
213
     * @return EventDefinition
214
     */
215
    public function getEventDefinition(): EventDefinition
216
    {
217
        return self::getDefinition()->getEventFromFullIdentifier($this->getEvent());
218
    }
219
220
    /**
221
     * Returns the event configuration stored as a FlexForm string.
222
     *
223
     * @return array
224
     */
225
    public function getEventConfiguration(): array
226
    {
227
        if (null === $this->eventConfiguration) {
228
            /** @var FlexFormService $flexFormService */
229
            $flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
230
231
            $this->eventConfiguration = $flexFormService->convertFlexFormContentToArray($this->eventConfigurationFlex);
232
        }
233
234
        return $this->eventConfiguration;
235
    }
236
237
    /**
238
     * @return BackendUser
239
     */
240
    public function getBackendUser()
241
    {
242
        return $this->backendUser;
243
    }
244
245
    /**
246
     * @return bool
247
     */
248
    public static function isCreatable(): bool
249
    {
250
        return Container::getBackendUser()
251
            && Container::getBackendUser()->check('tables_modify', self::getTableName());
252
    }
253
254
    /**
255
     * @param string $selectedEvent
256
     * @return string
257
     */
258
    public static function getCreationUri(string $selectedEvent = null): string
259
    {
260
        $tableName = static::getTableName();
261
262
        /** @var UriBuilder $uriBuilder */
263
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
264
265
        $href = (string)$uriBuilder->buildUriFromRoute(
266
            'record_edit',
267
            [
268
                "edit[$tableName][0]" => 'new',
269
                'returnUrl' => GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'),
270
            ]
271
        );
272
273
        if ($selectedEvent) {
274
            $href .= "&selectedEvent=$selectedEvent";
275
        }
276
277
        return $href;
278
    }
279
280
    /**
281
     * @return bool
282
     */
283
    public function isEditable(): bool
284
    {
285
        $backendUser = Container::getBackendUser();
286
287
        if (!$backendUser) {
0 ignored issues
show
introduced by
$backendUser is of type TYPO3\CMS\Core\Authentic...ckendUserAuthentication, thus it always evaluated to true.
Loading history...
288
            return false;
289
        }
290
291
        if (!$backendUser->check('tables_modify', self::getTableName())) {
292
            return false;
293
        }
294
295
        if (!$backendUser->recordEditAccessInternals(self::getTableName(), $this->uid)) {
296
            return false;
297
        }
298
299
        if ($this->pid === 0) {
300
            return true;
301
        }
302
303
        $page = Container::getPageRepository()->getPage($this->pid);
304
        $userPermissionOnPage = $backendUser->calcPerms($page);
305
306
        return (bool)($userPermissionOnPage & Permission::CONTENT_EDIT);
307
    }
308
309
    /**
310
     * @return string
311
     */
312
    public function getEditionUri(): string
313
    {
314
        $identifier = $this->getNotificationDefinition()->getIdentifier();
315
        $tableName = static::getTableName();
316
        $uid = $this->getUid();
317
318
        /** @var UriBuilder $uriBuilder */
319
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
320
321
        return (string)$uriBuilder->buildUriFromRoute(
322
            'record_edit',
323
            [
324
                "edit[$tableName][$uid]" => 'edit',
325
                'returnUrl' => GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL') . "#$identifier-$uid",
326
            ]
327
        );
328
    }
329
330
    /**
331
     * @return bool
332
     */
333
    public static function isListable(): bool
334
    {
335
        return Container::getBackendUser()
336
            && Container::getBackendUser()->check('tables_select', self::getTableName());
337
    }
338
339
    /**
340
     * @return bool
341
     */
342
    public function isViewable(): bool
343
    {
344
        return self::isListable();
345
    }
346
347
    /**
348
     * @return string
349
     */
350
    public function getViewUri(): string
351
    {
352
        $notificationDefinition = $this->getNotificationDefinition();
353
354
        $controller = 'Backend\\Manager\\Notification\\Show' . ucfirst($notificationDefinition->getIdentifier());
355
356
        /** @var ManagerModuleHandler $managerModuleHandler */
357
        $managerModuleHandler = Container::get(ManagerModuleHandler::class);
358
359
        return (string)$managerModuleHandler
360
            ->getUriBuilder()
361
            ->forController($controller)
362
            ->forAction('show')
363
            ->withArguments(['notificationIdentifier' => $this->getUid()])
364
            ->build();
365
    }
366
367
    /**
368
     * The selected channel is stored in the `$channel` property.
369
     *
370
     * @inheritdoc
371
     */
372
    public function shouldDispatch(ChannelDefinition $definition): bool
373
    {
374
        return $definition->getClassName() === $this->getChannel();
375
    }
376
377
    /**
378
     * Returns the name of the table for this notification. It is fetched in the
379
     * global TypoScript configuration.
380
     *
381
     * @return string
382
     */
383
    public static function getTableName(): string
384
    {
385
        /** @var ConfigurationManagerInterface $configurationManager */
386
        $configurationManager = Container::get(ConfigurationManagerInterface::class);
387
        $configuration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
388
389
        $className = self::getDefinition()
390
            ->getNotification(static::getDefinitionIdentifier())
391
            ->getClassName();
392
393
        return ArrayUtility::getValueByPath($configuration, "persistence/classes/$className/mapping/tableName");
394
    }
395
396
    /**
397
     * @return string
398
     */
399
    abstract public static function getDefinitionIdentifier(): string;
400
401
    /**
402
     * @return Definition
403
     */
404
    protected static function getDefinition(): Definition
405
    {
406
        return DefinitionService::get()->getDefinition();
0 ignored issues
show
Bug introduced by
It seems like getDefinition() 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

406
        return DefinitionService::get()->/** @scrutinizer ignore-call */ getDefinition();
Loading history...
407
    }
408
}
409