Passed
Pull Request — master (#125)
by Nathan
03:17
created

EntityNotification::isViewable()   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 $this->getDefinition()->getNotification(static::getDefinitionIdentifier());
152
    }
153
154
    /**
155
     * @return EventDefinition
156
     */
157
    public function getEventDefinition()
158
    {
159
        return $this->getDefinition()->getEventFromFullIdentifier($this->getEvent());
160
    }
161
162
    /**
163
     * Returns the event configuration stored as a FlexForm string.
164
     *
165
     * @return array
166
     */
167
    public function getEventConfiguration()
168
    {
169
        if (null === $this->eventConfiguration) {
170
            /** @var FlexFormService $flexFormService */
171
            $flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
172
173
            $this->eventConfiguration = $flexFormService->convertFlexFormContentToArray($this->eventConfigurationFlex);
174
        }
175
176
        return $this->eventConfiguration;
177
    }
178
179
    /**
180
     * @return BackendUser
181
     */
182
    public function getBackendUser()
183
    {
184
        return $this->backendUser;
185
    }
186
187
    /**
188
     * @return bool
189
     */
190
    public static function isCreatable()
191
    {
192
        return Container::getBackendUser()
193
            && Container::getBackendUser()->check('tables_modify', self::getTableName());
194
    }
195
196
    /**
197
     * @param string $selectedEvent
198
     * @return string
199
     */
200
    public static function getCreationUri($selectedEvent = null)
201
    {
202
        $tableName = static::getTableName();
203
204
        $href = BackendUtility::getModuleUrl(
205
            'record_edit',
206
            [
207
                "edit[$tableName][0]" => 'new',
208
                'returnUrl' => GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'),
209
            ]
210
        );
211
212
        if ($selectedEvent) {
213
            $href .= "&selectedEvent=$selectedEvent";
214
        }
215
216
        return $href;
217
    }
218
219
    /**
220
     * @return bool
221
     */
222
    public function isEditable()
223
    {
224
        $backendUser = Container::getBackendUser();
225
        $page = Container::getPageRepository()->getPage($this->pid);
226
        $userPermissionOnPage = $backendUser->calcPerms($page);
227
228
        return $backendUser->recordEditAccessInternals(self::getTableName(), $this->uid)
229
            && ($this->pid === 0
230
                || (bool)($userPermissionOnPage & Permission::CONTENT_EDIT)
231
            );
232
    }
233
234
    /**
235
     * @return string
236
     */
237
    public function getEditionUri()
238
    {
239
        $identifier = $this->getNotificationDefinition()->getIdentifier();
240
        $tableName = static::getTableName();
241
        $uid = $this->getUid();
242
243
        return BackendUtility::getModuleUrl(
244
            'record_edit',
245
            [
246
                "edit[$tableName][$uid]" => 'edit',
247
                'returnUrl' => GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL') . "#$identifier-$uid",
248
            ]
249
        );
250
    }
251
252
    /**
253
     * @return bool
254
     */
255
    public static function isListable()
256
    {
257
        return Container::getBackendUser()
258
            && Container::getBackendUser()->check('tables_select', self::getTableName());
259
    }
260
261
    /**
262
     * @return bool
263
     */
264
    public function isViewable()
265
    {
266
        return self::isListable();
267
    }
268
269
    /**
270
     * @return string
271
     */
272
    public function getViewUri()
273
    {
274
        $notificationDefinition = $this->getNotificationDefinition();
275
276
        $controller = 'Backend\\Manager\\Notification\\Show' . ucfirst($notificationDefinition->getIdentifier());
277
278
        $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...
279
280
        return $indexModuleHandler
281
            ->getUriBuilder()
282
            ->forController($controller)
283
            ->forAction('show')
284
            ->withArguments(['notificationIdentifier' => $this->getUid()])
285
            ->build();
286
    }
287
288
    /**
289
     * The selected channel is stored in the `$channel` property.
290
     *
291
     * @inheritdoc
292
     */
293
    public function shouldDispatch(ChannelDefinition $definition)
294
    {
295
        return $definition->getClassName() === $this->getChannel();
296
    }
297
298
    /**
299
     * Returns the name of the table for this notification. It is fetched in the
300
     * global TypoScript configuration.
301
     *
302
     * @return string
303
     */
304
    public static function getTableName()
305
    {
306
        /** @var ConfigurationManagerInterface $configurationManager */
307
        $configurationManager = Container::get(ConfigurationManagerInterface::class);
308
        $configuration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
309
310
        $className = self::getDefinition()
311
            ->getNotification(static::getDefinitionIdentifier())
312
            ->getClassName();
313
314
        return ArrayUtility::getValueByPath($configuration, "persistence/classes/$className/mapping/tableName");
315
    }
316
317
    /**
318
     * @return string
319
     */
320
    abstract public static function getDefinitionIdentifier();
321
322
    /**
323
     * @return Definition
324
     */
325
    protected static function getDefinition()
326
    {
327
        return DefinitionService::get()->getDefinition();
328
    }
329
}
330