Completed
Push — master ( 9b5529...8e3c70 )
by vistart
09:42 queued 03:37
created

NotificationReadTrait::vacuum()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 8
nc 3
nop 2
crap 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\BaseUserModel;
16
17
/**
18
 * Description of NotificationLog
19
 *
20
 * @author vistart <[email protected]>
21
 */
22
trait NotificationReadTrait
23
{
24
25
    /**
26
     * 
27
     * @param BaseUserModel $user
28
     * @param {$this->notificationClass} $notification
0 ignored issues
show
Documentation introduced by
The doc-type {$this->notificationClass} could not be parsed: Unknown type name "{$this-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
     * @return boolean False if notification was marked as read or it didn't exist.
30
     */
31 3
    public static function read($user, $notification)
32
    {
33 3
        if (empty($notification) || !$notification->findByIdentity($user)->guid($notification->guid)->one()) {
34 1
            return false;
35
        }
36 3
        $log = static::findByIdentity($user)->content($notification->guid)->one();
37 3
        if (!$log) {
38 3
            $log = $user->create(static::className(), ['content' => $notification->guid]);
39 3
        }
40 3
        return $log->save();
41
    }
42
43
    /**
44
     * 
45
     * @param BaseUserModel $user
46
     * @param {$this->notificationClass} $notification
0 ignored issues
show
Documentation introduced by
The doc-type {$this->notificationClass} could not be parsed: Unknown type name "{$this-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
47
     * @return boolean True if notification was marked as unread or it didn't exist.
48
     */
49 1
    public static function unread($user, $notification)
50
    {
51 1
        if (empty($notification) || !$notification->findByIdentity($user)->guid($notification->guid)->one()) {
52 1
            return true;
53
        }
54 1
        $log = static::findByIdentity($user)->content($notification->guid)->one();
55 1
        if ($log) {
56 1
            return $log->delete() == 1;
57
        }
58 1
        return true;
59
    }
60
61
    /**
62
     * Vacuum all the invalid notification read records.
63
     * @param BaseUserModel $user
64
     * @param string $notificationClass
65
     * @return integer The sum of notification read records vacuumed.
66
     */
67 2
    public static function vacuum($user, $notificationClass)
68
    {
69 2
        $logs = static::findByIdentity($user)->all();
70 2
        $count = 0;
71 2
        foreach ($logs as $log) {
72 2
            if (is_string($notificationClass) && ((int) ($notificationClass::findByIdentity($user)->guid($log->content)->count())) > 0) {
73 2
                continue;
74
            }
75 1
            $count += $log->delete();
76 2
        }
77 2
        return $count;
78
    }
79
}
80