Passed
Push — master ( c2c83c...eb4088 )
by Nathan
03:39
created

EntityNotification::isActive()   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\Domain\Notification;
18
19
use CuyZ\Notiz\Backend\Module\ManagerModuleHandler;
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\Channel\ChannelDefinition;
24
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
25
use CuyZ\Notiz\Core\Notification\MultipleChannelsNotification;
26
use CuyZ\Notiz\Core\Notification\Notification;
27
use CuyZ\Notiz\Service\Container;
28
use TYPO3\CMS\Backend\Utility\BackendUtility;
29
use TYPO3\CMS\Core\Type\Bitmask\Permission;
30
use TYPO3\CMS\Core\Utility\ArrayUtility;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
33
use TYPO3\CMS\Extbase\Domain\Model\BackendUser;
34
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
35
use TYPO3\CMS\Extbase\Service\FlexFormService;
36
37
abstract class EntityNotification extends AbstractEntity implements Notification, MultipleChannelsNotification
38
{
39
    /**
40
     * @var string
41
     */
42
    protected $title;
43
44
    /**
45
     * @var string
46
     */
47
    protected $description;
48
49
    /**
50
     * @var string
51
     */
52
    protected $event;
53
54
    /**
55
     * @var string
56
     */
57
    protected $channel;
58
59
    /**
60
     * @var string
61
     */
62
    protected $eventConfigurationFlex;
63
64
    /**
65
     * @var array
66
     */
67
    protected $eventConfiguration;
68
69
    /**
70
     * @var \TYPO3\CMS\Extbase\Domain\Model\BackendUser
71
     * @lazy
72
     */
73
    protected $backendUser;
74
75
    /**
76
     * @var bool
77
     */
78
    protected $hidden;
79
80
    /**
81
     * @return string|null
82
     */
83
    public function getTitle()
84
    {
85
        return $this->title;
86
    }
87
88
    /**
89
     * @param string $title
90
     */
91
    public function setTitle($title)
92
    {
93
        $this->title = $title;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getDescription()
100
    {
101
        return $this->description;
102
    }
103
104
    /**
105
     * @param string $description
106
     */
107
    public function setDescription($description)
108
    {
109
        $this->description = $description;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getEvent()
116
    {
117
        return $this->event;
118
    }
119
120
    /**
121
     * @param string $event
122
     */
123
    public function setEvent($event)
124
    {
125
        $this->event = $event;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getChannel()
132
    {
133
        return $this->channel;
134
    }
135
136
    /**
137
     * @param string $channel
138
     */
139
    public function setChannel($channel)
140
    {
141
        $this->channel = $channel;
142
    }
143
144
    /**
145
     * @param string $eventConfigurationFlex
146
     */
147
    public function setEventConfigurationFlex($eventConfigurationFlex)
148
    {
149
        $this->eventConfigurationFlex = $eventConfigurationFlex;
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    public function isActive()
156
    {
157
        return !$this->hidden;
158
    }
159
160
    /**
161
     * @param bool $active
162
     * @return void
163
     */
164
    public function setActive($active)
165
    {
166
        $this->hidden = !$active;
167
    }
168
169
    /**
170
     * @param EventDefinition|null $eventDefinition
171
     * @return string
172
     */
173
    public function getSwitchActivationUri(EventDefinition $eventDefinition = null)
174
    {
175
        $managerModuleHandler = Container::get(ManagerModuleHandler::class);
176
177
        return $managerModuleHandler
178
            ->getUriBuilder()
179
            ->forController('Backend\\Manager\\NotificationActivation')
180
            ->forAction('process')
181
            ->withArguments([
182
                'notificationType' => $this->getNotificationDefinition()->getIdentifier(),
183
                'notificationIdentifier' => $this->getUid(),
184
                'filterEvent' => $eventDefinition->getFullIdentifier(),
0 ignored issues
show
Bug introduced by
The method getFullIdentifier() does not exist on null. ( Ignorable by Annotation )

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

184
                'filterEvent' => $eventDefinition->/** @scrutinizer ignore-call */ getFullIdentifier(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
185
            ]);
186
    }
187
188
    /**
189
     * @return NotificationDefinition
190
     */
191
    public function getNotificationDefinition()
192
    {
193
        return self::getDefinition()->getNotification(static::getDefinitionIdentifier());
194
    }
195
196
    /**
197
     * @return bool
198
     */
199
    public function hasEventDefinition()
200
    {
201
        return self::getDefinition()->hasEventFromFullIdentifier($this->getEvent());
202
    }
203
204
    /**
205
     * @return EventDefinition
206
     */
207
    public function getEventDefinition()
208
    {
209
        return self::getDefinition()->getEventFromFullIdentifier($this->getEvent());
210
    }
211
212
    /**
213
     * Returns the event configuration stored as a FlexForm string.
214
     *
215
     * @return array
216
     */
217
    public function getEventConfiguration()
218
    {
219
        if (null === $this->eventConfiguration) {
220
            /** @var FlexFormService $flexFormService */
221
            $flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
222
223
            $this->eventConfiguration = $flexFormService->convertFlexFormContentToArray($this->eventConfigurationFlex);
224
        }
225
226
        return $this->eventConfiguration;
227
    }
228
229
    /**
230
     * @return BackendUser
231
     */
232
    public function getBackendUser()
233
    {
234
        return $this->backendUser;
235
    }
236
237
    /**
238
     * @return bool
239
     */
240
    public static function isCreatable()
241
    {
242
        return Container::getBackendUser()
243
            && Container::getBackendUser()->check('tables_modify', self::getTableName());
244
    }
245
246
    /**
247
     * @param string $selectedEvent
248
     * @return string
249
     */
250
    public static function getCreationUri($selectedEvent = null)
251
    {
252
        $tableName = static::getTableName();
253
254
        $href = BackendUtility::getModuleUrl(
255
            'record_edit',
256
            [
257
                "edit[$tableName][0]" => 'new',
258
                'returnUrl' => GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'),
259
            ]
260
        );
261
262
        if ($selectedEvent) {
263
            $href .= "&selectedEvent=$selectedEvent";
264
        }
265
266
        return $href;
267
    }
268
269
    /**
270
     * @return bool
271
     */
272
    public function isEditable()
273
    {
274
        $backendUser = Container::getBackendUser();
275
        $page = Container::getPageRepository()->getPage($this->pid);
276
        $userPermissionOnPage = $backendUser->calcPerms($page);
277
278
        return $backendUser->recordEditAccessInternals(self::getTableName(), $this->uid)
279
            && ($this->pid === 0
280
                || (bool)($userPermissionOnPage & Permission::CONTENT_EDIT)
281
            );
282
    }
283
284
    /**
285
     * @return string
286
     */
287
    public function getEditionUri()
288
    {
289
        $identifier = $this->getNotificationDefinition()->getIdentifier();
290
        $tableName = static::getTableName();
291
        $uid = $this->getUid();
292
293
        return BackendUtility::getModuleUrl(
294
            'record_edit',
295
            [
296
                "edit[$tableName][$uid]" => 'edit',
297
                'returnUrl' => GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL') . "#$identifier-$uid",
298
            ]
299
        );
300
    }
301
302
    /**
303
     * @return bool
304
     */
305
    public static function isListable()
306
    {
307
        return Container::getBackendUser()
308
            && Container::getBackendUser()->check('tables_select', self::getTableName());
309
    }
310
311
    /**
312
     * @return bool
313
     */
314
    public function isViewable()
315
    {
316
        return self::isListable();
317
    }
318
319
    /**
320
     * @return string
321
     */
322
    public function getViewUri()
323
    {
324
        $notificationDefinition = $this->getNotificationDefinition();
325
326
        $controller = 'Backend\\Manager\\Notification\\Show' . ucfirst($notificationDefinition->getIdentifier());
327
328
        $managerModuleHandler = Container::get(ManagerModuleHandler::class);
329
330
        return $managerModuleHandler
331
            ->getUriBuilder()
332
            ->forController($controller)
333
            ->forAction('show')
334
            ->withArguments(['notificationIdentifier' => $this->getUid()])
335
            ->build();
336
    }
337
338
    /**
339
     * The selected channel is stored in the `$channel` property.
340
     *
341
     * @inheritdoc
342
     */
343
    public function shouldDispatch(ChannelDefinition $definition)
344
    {
345
        return $definition->getClassName() === $this->getChannel();
346
    }
347
348
    /**
349
     * Returns the name of the table for this notification. It is fetched in the
350
     * global TypoScript configuration.
351
     *
352
     * @return string
353
     */
354
    public static function getTableName()
355
    {
356
        /** @var ConfigurationManagerInterface $configurationManager */
357
        $configurationManager = Container::get(ConfigurationManagerInterface::class);
358
        $configuration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
359
360
        $className = self::getDefinition()
361
            ->getNotification(static::getDefinitionIdentifier())
362
            ->getClassName();
363
364
        return ArrayUtility::getValueByPath($configuration, "persistence/classes/$className/mapping/tableName");
365
    }
366
367
    /**
368
     * @return string
369
     */
370
    abstract public static function getDefinitionIdentifier();
371
372
    /**
373
     * @return Definition
374
     */
375
    protected static function getDefinition()
376
    {
377
        return DefinitionService::get()->getDefinition();
378
    }
379
}
380