NotificationCell::notifications()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.0303
c 0
b 0
f 0
cc 3
eloc 29
nc 2
nop 0
1
<?php
2
namespace App\View\Cell;
3
4
use Cake\Core\Configure;
5
use Cake\View\Cell;
6
7
class NotificationCell extends Cell
8
{
9
    /**
10
     * Display the notifications.
11
     *
12
     * @return \Cake\Network\Response
13
     */
14
    public function notifications()
15
    {
16
        $this->loadModel('Notifications');
17
18
        $notifications = $this->Notifications
0 ignored issues
show
Bug introduced by
The property Notifications does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
            ->find()
20
            ->where([
21
                'user_id' => $this->request->session()->read('Auth.User.id')
22
            ])
23
            ->order([
24
                'is_read' => 'ASC',
25
                'created' => 'DESC'
26
            ])
27
            ->limit(Configure::read('User.max_notifications'))
28
            ->find('map', [
29
                'session' => $this->request->session()
30
            ])
31
            ->toArray();
32
33
        $statistics = [
34
            'read' => 0,
35
            'unread' => 0
36
        ];
37
38
        //A map function to count the read/unread notifications
39
        $map = function ($v) {
40
            if ($v->is_read == 0) {
41
                return 'unread';
42
            } else {
43
                return 'read';
44
            }
45
        };
46
47
        $statistics = array_merge(
48
            $statistics,
49
            array_count_values(
50
                array_map(
51
                    $map,
52
                    $notifications
53
                )
54
            )
55
        );
56
57
        $hasNewNotifs = ($statistics['unread'] >= 1) ? true : false;
58
59
        $this->set(compact('notifications', 'statistics', 'hasNewNotifs'));
60
    }
61
}
62