Completed
Push — version-4 ( 2da0bd...27f202 )
by
unknown
02:09
created

Notification::isAnonymous()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Fenos\Notifynder\Models;
4
5
use Fenos\Notifynder\Builder\Notification as BuilderNotification;
6
use Fenos\Notifynder\Parsers\NotificationParser;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Model;
9
10
class Notification extends Model
11
{
12
    protected $fillable = [
13
        'to_id',
14
        'to_type',
15
        'from_id',
16
        'from_type',
17
        'category_id',
18
        'read',
19
        'url',
20
        'extra',
21
        'expires_at',
22
        'stack_id',
23
    ];
24
25
    protected $appends = [
26
        'text',
27
    ];
28
29
    protected $casts = [
30
        'extra' => 'array',
31
    ];
32
33
    public function __construct($attributes = [])
34
    {
35
        $this->fillable($this->mergeFillables());
36
37
        if ($attributes instanceof BuilderNotification) {
38
            $attributes = $attributes->toArray();
39
        }
40
41
        parent::__construct($attributes);
42
    }
43
44
    public function category()
45
    {
46
        return $this->belongsTo(NotificationCategory::class, 'category_id');
47
    }
48
49
    public function from()
50
    {
51
        if (notifynder_config()->isPolymorphic()) {
52
            return $this->belongsTo(notifynder_config()->getModel(), 'from_id');
53
        }
54
55
        return $this->morphTo();
56
    }
57
58
    public function to()
59
    {
60
        if (notifynder_config()->isPolymorphic()) {
61
            return $this->belongsTo(notifynder_config()->getModel(), 'to_id');
62
        }
63
64
        return $this->morphTo();
65
    }
66
67
    public function getCustomFillableFields()
68
    {
69
        return notifynder_config()->getAdditionalFields();
70
    }
71
72
    protected function mergeFillables()
73
    {
74
        $fillables = array_unique($this->getFillable() + $this->getCustomFillableFields());
75
76
        return $fillables;
77
    }
78
79
    public function getTemplateBodyAttribute()
80
    {
81
        if (notifynder_config()->isTranslated()) {
82
            $key = notifynder_config()->getTranslationDomain().'.'.$this->category->name;
0 ignored issues
show
Documentation introduced by
The property category does not exist on object<Fenos\Notifynder\Models\Notification>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
83
            $trans = trans($key);
84
            if ($trans != $key) {
85
                return $trans;
86
            }
87
        }
88
89
        return $this->category->text;
0 ignored issues
show
Documentation introduced by
The property category does not exist on object<Fenos\Notifynder\Models\Notification>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
90
    }
91
92
    public function getTextAttribute()
93
    {
94
        if (! array_key_exists('text', $this->attributes)) {
95
            $notifynderParse = new NotificationParser();
96
            $this->attributes['text'] = $notifynderParse->parse($this);
97
        }
98
99
        return $this->attributes['text'];
100
    }
101
102
    public function read()
103
    {
104
        $this->update(['read' => 1]);
105
    }
106
107
    public function unread()
108
    {
109
        $this->update(['read' => 0]);
110
    }
111
112
    public function resend()
113
    {
114
        $this->updateTimestamps();
115
        $this->read = 0;
0 ignored issues
show
Documentation introduced by
The property read does not exist on object<Fenos\Notifynder\Models\Notification>. 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...
116
117
        return $this->save();
118
    }
119
120
    public function isAnonymous()
121
    {
122
        return is_null($this->from_id);
0 ignored issues
show
Documentation introduced by
The property from_id does not exist on object<Fenos\Notifynder\Models\Notification>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
123
    }
124
125 View Code Duplication
    public function scopeByCategory(Builder $query, $category)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $categoryId = $category;
128
        if ($category instanceof NotificationCategory) {
129
            $categoryId = $category->getKey();
130
        } elseif (! is_numeric($category)) {
131
            $categoryId = NotificationCategory::byName($category)->firstOrFail()->getKey();
0 ignored issues
show
Bug introduced by
The method byName() does not exist on Fenos\Notifynder\Models\NotificationCategory. Did you maybe mean scopeByName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
132
        }
133
134
        return $query->where('category_id', $categoryId);
135
    }
136
137
    public function scopeByRead(Builder $query, $read = 1)
138
    {
139
        return $query->where('read', $read);
140
    }
141
}
142