Completed
Pull Request — master (#17)
by vistart
06:15
created

NotificationReadTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 95.83%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 0
cbo 3
dl 0
loc 58
ccs 23
cts 24
cp 0.9583
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 11 4
A unread() 0 11 4
A vacuum() 0 12 4
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license https://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
use vistart\Models\models\BaseMongoNotificationModel;
16
use vistart\Models\models\BaseNotificationModel;
17
use vistart\Models\models\BaseUserModel;
18
19
/**
20
 * Description of NotificationLog
21
 *
22
 * @author vistart <[email protected]>
23
 */
24
trait NotificationReadTrait
25
{
26
27
    /**
28
     * 
29
     * @param BaseUserModel $user
30
     * @param BaseMongoNotificationModel|BaseNotificationModel $notification
31
     * @return boolean False if notification was marked as read or it didn't exist.
32
     */
33 3
    public static function read($user, $notification)
34
    {
35 3
        if (empty($notification) || !$notification->findByIdentity($user)->guid($notification->guid)->one()) {
36 1
            return false;
37
        }
38 3
        $log = static::findByIdentity($user)->content($notification->guid)->one();
39 3
        if (!$log) {
40 3
            $log = $user->create(static::className(), ['content' => $notification->guid]);
41 3
        }
42 3
        return $log->save();
43
    }
44
45
    /**
46
     * 
47
     * @param BaseUserModel $user
48
     * @param BaseMongoNotificationModel|BaseNotificationModel $notification
49
     * @return boolean True if notification was marked as unread or it didn't exist.
50
     */
51 3
    public static function unread($user, $notification)
52
    {
53 1
        if (empty($notification) || !$notification->findByIdentity($user)->guid($notification->guid)->one()) {
54 1
            return true;
55
        }
56 1
        $log = static::findByIdentity($user)->content($notification->guid)->one();
57 1
        if ($log) {
58 1
            return $log->delete() == 1;
59
        }
60 3
        return true;
61
    }
62
63
    /**
64
     * Vacuum all the invalid notification read records.
65
     * @param BaseUserModel $user
66
     * @param string $notificationClass
67
     * @return integer The sum of notification read records vacuumed.
68
     */
69 2
    public static function vacuum($user, $notificationClass)
70
    {
71 2
        $logs = static::findByIdentity($user)->all();
72 2
        $count = 0;
73 2
        foreach ($logs as $log) {
74 2
            if (is_string($notificationClass) && ((int) ($notificationClass::findByIdentity($user)->guid($log->content)->count())) > 0) {
75 2
                continue;
76
            }
77
            $count += $log->delete();
78 2
        }
79 2
        return $count;
80
    }
81
}
82