Completed
Push — develop ( 2606e4...9302c9 )
by Neil
04:35
created

Notification   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 79
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A markRead() 0 3 1
A markSticky() 0 3 1
A setAttrib() 0 13 2
A scopeIsUnread() 0 4 1
A scopeIsArchived() 0 5 1
A scopeLimit() 0 4 1
A scopeSource() 0 4 1
A attribs() 0 3 1
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Notification extends Model
8
{
9
10
    /**
11
     * The table associated with the model.
12
     *
13
     * @var string
14
     */
15
    protected $table = 'notifications';
16
17
    /**
18
     * The primary key column name.
19
     *
20
     * @var string
21
     */
22
    protected $primaryKey = 'notifications_id';
23
24
    /**
25
     * Indicates if the model should be timestamped.
26
     *
27
     * @var bool
28
     */
29
    public $timestamps = false;
30
31
32
    /**
33
     * Mark this notifcation as read or unread
34
     *
35
     * @var bool
36
     */
37
    public function markRead($enabled) {
38
        $this->setAttrib('read', $enabled);
39
    }
40
41
    public function markSticky($enabled) {
42
        $this->setAttrib('sticky', $enabled);
43
    }
44
45
    private function setAttrib($name, $enabled) {
46
        if ($enabled === true) {
47
            $read = new NotificationAttrib;
48
            $read->user_id          = \Auth::user()->user_id;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<App\Models\NotificationAttrib>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
            $read->key              = $name;
0 ignored issues
show
Documentation introduced by
The property key does not exist on object<App\Models\NotificationAttrib>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
50
            $read->value            = 1;
0 ignored issues
show
Documentation introduced by
The property value does not exist on object<App\Models\NotificationAttrib>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
52
            return $this->attribs()->save($read);
53
        }
54
        else {
55
            return $this->attribs()->where('key', $name)->delete();
56
        }
57
    }
58
59
60
    public function scopeIsUnread($query)
61
    {
62
        return $query->leftJoin('notifications_attribs', 'notifications.notifications_id', '=', 'notifications_attribs.notifications_id')->source()->whereNull('notifications_attribs.user_id')->orWhere(['key'=>'sticky', 'value'=> 1])->limit();
63
    }
64
65
    public function scopeIsArchived($query, $request)
66
    {
67
        $user_id = $request->user()->user_id;
68
        return $query->leftJoin('notifications_attribs', 'notifications.notifications_id', '=', 'notifications_attribs.notifications_id')->source()->where('notifications_attribs.user_id', $user_id)->where(['key'=>'read', 'value'=> 1])->limit();
69
    }
70
71
    public function scopeLimit($query)
72
    {
73
        return $query->select('notifications.*','key', 'users.username');
74
    }
75
76
    public function scopeSource($query)
77
    {
78
        return $query->leftJoin('users', 'notifications.source', '=', 'users.user_id');
79
    }
80
81
    public function attribs() {
82
        return $this->hasMany('App\Models\NotificationAttrib', 'notifications_id', 'notifications_id');
83
    }
84
85
}
86