Notifications::totalUnRead()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\Models;
5
6
use Phalcon\Di;
7
8
class Notifications extends AbstractModel
9
{
10
    /**
11
     *
12
     * @var integer
13
     */
14
    public $id;
15
16
    /**
17
     *
18
     * @var integer
19
     */
20
    public $from_users_id;
21
22
    /**
23
     *
24
     * @var integer
25
     */
26
    public $users_id;
27
28
    /**
29
     *
30
     * @var integer
31
     */
32
    public $companies_id;
33
34
    /**
35
    *
36
    * @var integer
37
    */
38
    public $apps_id;
39
40
    /**
41
     *
42
     * @var integer
43
     */
44
    public $system_modules_id;
45
46
    /**
47
     *
48
     * @var integer
49
     */
50
    public $notification_type_id;
51
52
    /**
53
     *
54
     * @var integer
55
     */
56
    public $entity_id;
57
58
    /**
59
     *
60
     * @var string
61
     */
62
    public $content;
63
64
    /**
65
     *
66
     * @var integer
67
     */
68
    public $read;
69
70
    /**
71
     *
72
     * @var integer
73
     */
74
    public $is_deleted;
75
76
    /**
77
     *
78
     * @var string
79
     */
80
    public $created_at;
81
82
    /**
83
     *
84
     * @var string
85
     */
86
    public $updated_at;
87
88
    /**
89
     * Initialize method for model.
90
     */
91
    public function initialize()
92
    {
93
        $this->setSource('notifications');
94
95
        $this->belongsTo(
96
            'users_id',
97
            'Canvas\Models\Users',
98
            'id',
99
            ['alias' => 'user']
100
        );
101
102
        $this->belongsTo(
103
            'from_users_id',
104
            'Canvas\Models\Users',
105
            'id',
106
            ['alias' => 'from']
107
        );
108
109
        $this->belongsTo(
110
            'notification_type_id',
111
            'Canvas\Models\NotificationType',
112
            'id',
113
            ['alias' => 'type']
114
        );
115
    }
116
117
    /**
118
     * Returns table name mapped in the model.
119
     *
120
     * @return string
121
     */
122
    public function getSource(): string
123
    {
124
        return 'notifications';
125
    }
126
127
    /**
128
     * Mark as Read all the notification from a user.
129
     *
130
     * @param Users $user
131
     * @return void
132
     */
133
    public static function markAsRead(Users $user): bool
134
    {
135
        $result = Di::getDefault()->getDb()->prepare(
136
            'UPDATE notifications set `read` = 1 WHERE users_id = ? AND companies_id = ? AND apps_id = ?'
137
        );
138
139
        $result->execute([
140
            $user->getId(),
141
            $user->currentCompanyId(),
142
            Di::getDefault()->getApp()->getId()
143
        ]);
144
145
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type void.
Loading history...
146
    }
147
148
    /**
149
     * Get the total notification for the current user.
150
     *
151
     * @return int
152
     */
153
    public static function totalUnRead(Users $user): int
154
    {
155
        return self::count([
156
            'conditions' => 'is_deleted = 0 AND read = 0 AND users_id = ?0 AND companies_id = ?1 AND apps_id = ?2',
157
            'bind' => [
158
                $user->getId(),
159
                $user->currentCompanyId(),
160
                Di::getDefault()->getApp()->getId()
161
            ]
162
        ]);
163
    }
164
}
165