AppCtrlNotification::portletView()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 78
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 59
c 1
b 0
f 1
dl 0
loc 78
rs 8.2723
cc 6
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
// -------------------------------------------------------------------------
4
// OVIDENTIA http://www.ovidentia.org
5
// Ovidentia is free software; you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation; either version 2, or (at your option)
8
// any later version.
9
//
10
// This program is distributed in the hope that it will be useful, but
11
// WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
// See the GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with this program; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18
// USA.
19
// -------------------------------------------------------------------------
20
/**
21
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
22
 * @copyright Copyright (c) 2022 by SI4YOU ({@link https://www.siforyou.com})
23
 */
24
namespace Capwelton\LibApp\Ctrl;
25
26
use Capwelton\Widgets\Widgets\Item\WidgetLink;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Item\WidgetLink 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 Capwelton\Widgets\Widgets\Layout\WidgetFlexLayout;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Layout\WidgetFlexLayout 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 Capwelton\LibApp\Set\AppNotification;
29
require_once APP_CTRL_PATH . '/record.ctrl.php';
30
31
/**
32
 * This controller manages actions that can be performed on notifications
33
 *
34
 * @method Func_App App()
35
 * @method app_NotificationSet getRecordSet()
36
 * @method self proxy()
37
 */
38
class AppCtrlNotification extends AppCtrlRecord
39
{
40
    
41
    public function popup($itemId = null)
42
    {
43
        $App = $this->App();
44
        $page = $App->Ui()->Page($itemId);
45
        $page->setTitle($App->translate('Notifications'));
46
        $page->setSizePolicy('testNotification');
47
        
48
        $W = bab_Widgets();
49
        
50
        $notificationSet = $App->NotificationSet();
51
        $notifications = $notificationSet->select($notificationSet->user->is($App->getCurrentUser())
52
            ->_AND_($notificationSet->seen->is(false)))
53
            ->orderDesc($notificationSet->createdOn);
54
        
55
        $page->addItem($notifBox = $W->VBoxLayout());
56
        $page->setReloadAction($this->proxy()
57
            ->popup($notifBox->getId()));
58
        $page->addClass(AppNotification::NOTIFICATION_PORTLET_CLASS);
59
        
60
        foreach ($notifications as $notification){
61
            $notifBox->addItem($W->Link($notification->title, $this->proxy()
0 ignored issues
show
Bug introduced by
It seems like $this->proxy()->display($notification->id) can also be of type true; however, parameter $url of Func_Widgets::Link() does only seem to accept Widget_Action|bab_url|string, maybe add an additional type check? ( Ignorable by Annotation )

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

61
            $notifBox->addItem($W->Link($notification->title, /** @scrutinizer ignore-type */ $this->proxy()
Loading history...
62
                ->display($notification->id))
63
                ->setOpenMode(WidgetLink::OPEN_DIALOG));
64
        }
65
        
66
        return $page;
67
    }
68
    
69
    public function display($id)
70
    {
71
        $set = $this->getRecordSet();
72
        $record = $set->get($id);
73
        if(! $record){
74
            return true;
75
        }
76
        
77
        $App = $this->App();
78
        $Ui = $App->Ui();
79
        $page = $Ui->Page();
80
        $page->setTitle($record->title);
81
        
82
        $record->markAsSeen();
83
        $W = bab_Widgets();
84
        $box = $W->VBoxItems();
85
        
86
        $box->addItem($W->Label($record->description));
87
        
88
        $box->addItem($W->Label(date('d/m/Y H:i', strtotime($record->createdOn))));
89
        
90
        $page->addItem($box);
91
        
92
        return $page;
93
    }
94
    
95
    public function _createDummy()
96
    {
97
        $App = $this->App();
98
        $notifSet = $App->NotificationSet();
99
        $dummy = $notifSet->newRecord();
100
        
101
        $dummy->title = 'Dummy title';
102
        $dummy->description = 'Lorem ipsum dolor sit amet, consectetur adisciping elit';
103
        $dummy->user = bab_getUserId();
104
        $dummy->save();
105
        
106
        $dummy->createSSENotification();
107
    }
108
    
109
    public function portletView($itemId = null)
110
    {
111
        $App = $this->App();
112
        $W = bab_Widgets();
113
        $notifSet = $App->NotificationSet();
114
        
115
        $box = $W->FlowItems()->setIconFormat(24, 'center');
116
        if(isset($itemId)){
117
            $box->setId($itemId);
118
        }
119
        $notifs = $notifSet->selectUserNotifications();
120
        $nbNotif = $notifs->count();
121
        $notifIcon = 'icon ';
122
        $notifClass = 'label label-default';
123
        $sizePolicy = 'notificationBtn';
124
        
125
        $box->addItem($btnWidget = $W->FlexItems())
0 ignored issues
show
Bug introduced by
The method FlexItems() does not exist on Func_Widgets. ( Ignorable by Annotation )

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

125
        $box->addItem($btnWidget = $W->/** @scrutinizer ignore-call */ FlexItems())

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...
126
            ->setIcon(16, 'left');
0 ignored issues
show
Unused Code introduced by
The call to Widget_Item::setIcon() has too many arguments starting with 'left'. ( Ignorable by Annotation )

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

126
            ->/** @scrutinizer ignore-call */ setIcon(16, 'left');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
127
        if($nbNotif > 0){
128
            $btnWidget->addItem($W->Label($nbNotif)
129
                ->addClass('notificationNumber'));
130
            $btnWidget->addClass('notificationDisplayButton');
131
            $notifIcon .= 'webapp-bell-ringing-o';
132
            $sizePolicy .= ' hasNotification';
133
            
134
            $notificationBox = $W->FlexItems($W->Html('')
135
                ->addClass('notificationListArrow'), $W->Html('')
136
                ->addClass('notificationListArrowInside'))
137
                ->setSizePolicy('notificationsList')
138
                ->setDirection(WidgetFlexLayout::FLEX_DIRECTION_COL);
139
            
140
            $notificationBox->addItems($W->FlexItems($W->Title($App->translate('Notifications'), 3), $W->Link('', $this->proxy()
141
                ->personalConfiguration())
142
                ->setOpenMode(WidgetLink::OPEN_DIALOG)
143
                ->addClass('icon webapp-gear')
144
                ->setTitle($App->translate('Notifications configuration')))
145
                ->addClass('notificationBoxTitle')
146
                ->setJustifyContent(WidgetFlexLayout::FLEX_JUSTIFY_CONTENT_SPACE_BETWEEN), $notificationBoxElements = $W->FlexItems()
147
                ->addClass('notificationBoxElements')
148
                ->setDirection(WidgetFlexLayout::FLEX_DIRECTION_COL));
149
            
150
            foreach ($notifs as $notif){
151
                $notificationBoxElements->addItem($W->Link($el = $W->FlexItems($W->Label($notif->title)
152
                    ->addClass('notificationElementTitle'), $W->Label(date('d/m/Y H:i', strtotime($notif->createdOn)))
153
                    ->addClass('notificationElementDate'))
154
                    ->setDirection(WidgetFlexLayout::FLEX_DIRECTION_COL), $this->proxy()
0 ignored issues
show
Bug introduced by
It seems like $this->proxy()->display($notif->id) can also be of type true; however, parameter $url of Func_Widgets::Link() does only seem to accept Widget_Action|bab_url|string, maybe add an additional type check? ( Ignorable by Annotation )

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

154
                    ->setDirection(WidgetFlexLayout::FLEX_DIRECTION_COL), /** @scrutinizer ignore-type */ $this->proxy()
Loading history...
155
                    ->display($notif->id))
156
                    ->addClass('notificationBoxElement')
157
                    ->setOpenMode(WidgetLink::OPEN_DIALOG));
158
                if(! empty($notif->description)){
159
                    $desc = $notif->description;
160
                    if(strlen($desc) > 40){
161
                        $desc = trim(substr($desc, 0, 40)) . '...';
162
                    }
163
                    $el->addItem($W->Label($desc)
164
                        ->addClass('notificationElementDescription'), 1);
165
                }
166
            }
167
            
168
            $notificationBoxElements->addItem($W->Label($App->translate('Show more'))
169
                ->addClass('notificationElementShowMore'));
170
            
171
            $box->addItem($notificationBox);
172
        }
173
        else{
174
            $notifIcon .= 'webapp-bell-o';
175
        }
176
        
177
        $btnWidget->addClass($notifIcon)->addClass($notifClass);
178
        $btnWidget->setSizePolicy($sizePolicy);
179
        
180
        $box->setReloadAction($this->proxy()
0 ignored issues
show
Bug introduced by
$this->proxy()->portletView($box->getId()) of type Widget_FlowLayout is incompatible with the type Widget_Action expected by parameter $reloadAction of Widget_Item::setReloadAction(). ( Ignorable by Annotation )

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

180
        $box->setReloadAction(/** @scrutinizer ignore-type */ $this->proxy()
Loading history...
181
            ->portletView($box->getId()));
182
        
183
        $box->addClass(AppNotification::NOTIFICATION_PORTLET_CLASS);
184
        $box->addClass('notificationsBox');
185
        
186
        return $box;
187
    }
188
    
189
    public function personalConfiguration()
190
    {
191
        $App = $this->App();
192
        $Ui = $App->Ui();
193
        
194
        $page = $Ui->Page();
195
        $page->setTitle($App->translate('Notifications configuration'));
196
        
197
        $markAllAsSeenEditor = $Ui->BasicEditor();
198
        $markAllAsSeenEditor->isAjax = true;
199
        $markAllAsSeenEditor->setSaveAction($this->proxy()
200
            ->markAllAsSeen(), $App->translate('Mark all notification as seen'));
201
        
202
        $page->addItem($markAllAsSeenEditor);
203
        
204
        return $page;
205
    }
206
    
207
    public function markAllAsSeen()
208
    {
209
        $this->requireSaveMethod();
210
        
211
        $App = $this->App();
212
        
213
        $notifSet = $App->NotificationSet();
214
        $notifs = $notifSet->selectUserNotifications();
215
        foreach ($notifs as $notif){
216
            $notif->markAsSeen();
217
        }
218
        
219
        return true;
220
    }
221
}
222