ShowNotificationDetailsButton::addButton()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 10
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\Backend\FormEngine\ButtonBar;
19
20
use CuyZ\Notiz\Core\Definition\DefinitionService;
21
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
22
use CuyZ\Notiz\Core\Notification\Viewable;
23
use CuyZ\Notiz\Domain\Notification\EntityNotification;
24
use CuyZ\Notiz\Service\LocalizationService;
25
use ReflectionClass;
26
use TYPO3\CMS\Backend\Controller\EditDocumentController;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Backend\Contro...\EditDocumentController 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...
27
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Backend\Template\Components\ButtonBar 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...
28
use TYPO3\CMS\Backend\Template\ModuleTemplate;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Backend\Template\ModuleTemplate 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...
29
use TYPO3\CMS\Core\Imaging\Icon;
30
use TYPO3\CMS\Core\Imaging\IconFactory;
31
use TYPO3\CMS\Core\SingletonInterface;
32
33
/**
34
 * Adds a button "View details" to the button bar at the top of the screen when
35
 * editing a notification record.
36
 *
37
 * Clicking this button loads the NotiZ module showing more information about
38
 * the current notification.
39
 */
40
class ShowNotificationDetailsButton implements SingletonInterface
41
{
42
    /**
43
     * @var DefinitionService
44
     */
45
    protected $definitionService;
46
47
    /**
48
     * @var IconFactory
49
     */
50
    protected $iconFactory;
51
52
    /**
53
     * @param DefinitionService $definitionService
54
     * @param IconFactory $iconFactory
55
     */
56
    public function __construct(DefinitionService $definitionService, IconFactory $iconFactory)
57
    {
58
        $this->definitionService = $definitionService;
59
        $this->iconFactory = $iconFactory;
60
    }
61
62
    /**
63
     * @param EditDocumentController $controller
64
     */
65
    public function addButton(EditDocumentController $controller)
66
    {
67
        if ($this->definitionService->getValidationResult()->hasErrors()) {
68
            return;
69
        }
70
71
        foreach ($this->definitionService->getDefinition()->getNotifications() as $notificationDefinition) {
72
            $notification = $this->getNotification($notificationDefinition, $controller);
73
74
            if ($notification) {
75
                $this->addButtonForNotification($controller, $notification);
76
77
                break;
78
            }
79
        }
80
    }
81
82
    /**
83
     * @param NotificationDefinition $notificationDefinition
84
     * @param EditDocumentController $controller
85
     * @return Viewable|null [PHP 7.1]
86
     */
87
    protected function getNotification(NotificationDefinition $notificationDefinition, EditDocumentController $controller)
88
    {
89
        /** @var EntityNotification|Viewable $className */
90
        $className = $notificationDefinition->getClassName();
91
92
        if (!in_array(Viewable::class, class_implements($className))
93
            || !in_array(EntityNotification::class, class_parents($className))
94
        ) {
95
            return null;
96
        }
97
98
        $tableName = $className::getTableName();
0 ignored issues
show
Bug introduced by
The method getTableName() does not exist on CuyZ\Notiz\Core\Notification\Viewable. Since it exists in all sub-types, consider adding an abstract or default implementation to CuyZ\Notiz\Core\Notification\Viewable. ( Ignorable by Annotation )

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

98
        /** @scrutinizer ignore-call */ 
99
        $tableName = $className::getTableName();
Loading history...
99
100
        if (!isset($controller->editconf[$tableName])) {
101
            return null;
102
        }
103
104
        $uid = reset(array_keys($controller->editconf[$tableName]));
105
106
        // We show the button only for existing records being edited.
107
        if ($controller->editconf[$tableName][$uid] !== 'edit') {
108
            return null;
109
        }
110
111
        /** @var Viewable $notification */
112
        $notification = $notificationDefinition->getProcessor()->getNotificationFromIdentifier((string)$uid);
113
114
        return $notification;
115
    }
116
117
    /**
118
     * @param EditDocumentController $controller
119
     * @param Viewable $notification
120
     */
121
    protected function addButtonForNotification(EditDocumentController $controller, Viewable $notification)
122
    {
123
        $buttonBar = $this->getModuleTemplate($controller)
124
            ->getDocHeaderComponent()
125
            ->getButtonBar();
126
127
        $button = $buttonBar->makeLinkButton()
128
            ->setShowLabelText(true)
129
            ->setHref($notification->getViewUri())
130
            ->setTitle(LocalizationService::localize('Notification/Entity:button_bar.view_details'))
131
            ->setIcon($this->iconFactory->getIcon(
132
                'actions-view',
133
                Icon::SIZE_SMALL
134
            ));
135
136
        $buttonBar->addButton($button, ButtonBar::BUTTON_POSITION_LEFT, 50);
137
    }
138
139
    /**
140
     * Unfortunately TYPO3 doesn't provide a public API to access the module
141
     * template and add an icon to it, so we need to cheat a bit.
142
     *
143
     * @param EditDocumentController $controller
144
     * @return ModuleTemplate
145
     */
146
    protected function getModuleTemplate(EditDocumentController $controller): ModuleTemplate
147
    {
148
        $reflection = new ReflectionClass($controller);
149
        $property = $reflection->getProperty('moduleTemplate');
150
        $property->setAccessible(true);
151
152
        /** @var ModuleTemplate $moduleTemplate */
153
        $moduleTemplate = $property->getValue($controller);
154
155
        return $moduleTemplate;
156
    }
157
}
158