Passed
Push — master ( 7bc499...a28b91 )
by Nathan
03:23
created

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