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

NotificationReadTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 58
ccs 24
cts 24
cp 1
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\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