Passed
Pull Request — master (#159)
by Nathan
03:58
created

EntityNotification::markAsActive()   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
     * @return void
162
     */
163
    public function markAsActive()
164
    {
165
        $this->hidden = false;
166
    }
167
168
    /**
169
     * @return void
170
     */
171
    public function markAsInactive()
172
    {
173
        $this->hidden = true;
174
    }
175
176
    /**
177
     * @param EventDefinition|null $eventDefinition
178
     * @return string
179
     */
180
    public function getSwitchActivationUri(EventDefinition $eventDefinition = null)
181
    {
182
        $managerModuleHandler = Container::get(ManagerModuleHandler::class);
183
184
        return $managerModuleHandler
185
            ->getUriBuilder()
186
            ->forController('Backend\\Manager\\NotificationActivation')
187
            ->forAction('process')
188
            ->withArguments([
189
                'notificationType' => $this->getNotificationDefinition()->getIdentifier(),
190
                'notificationIdentifier' => $this->getUid(),
191
                '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

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