Completed
Push — version-4 ( 9e921c...18159e )
by
unknown
05:03
created

Notification::getTemplateBodyAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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
/**
11
 * Class Notification.
12
 */
13
class Notification extends Model
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $fillable = [
19
        'to_id',
20
        'to_type',
21
        'from_id',
22
        'from_type',
23
        'category_id',
24
        'read',
25
        'url',
26
        'extra',
27
        'expires_at',
28
        'stack_id',
29
    ];
30
31
    /**
32
     * @var array
33
     */
34
    protected $appends = [
35
        'text',
36
    ];
37
38
    /**
39
     * @var array
40
     */
41
    protected $casts = [
42
        'extra' => 'array',
43
    ];
44
45
    /**
46
     * Notification constructor.
47
     *
48
     * @param array $attributes
49
     */
50
    public function __construct($attributes = [])
51
    {
52
        $this->fillable($this->mergeFillables());
53
54
        if ($attributes instanceof BuilderNotification) {
55
            $attributes = $attributes->toArray();
56
        }
57
58
        parent::__construct($attributes);
59
    }
60
61
    /**
62
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
63
     */
64
    public function category()
65
    {
66
        return $this->belongsTo(NotificationCategory::class, 'category_id');
67
    }
68
69
    /**
70
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
71
     */
72
    public function from()
73
    {
74
        if (notifynder_config()->isPolymorphic()) {
75
            return $this->morphTo('from');
76
        }
77
78
        return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'from_id');
79
    }
80
81
    /**
82
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
83
     */
84
    public function to()
85
    {
86
        if (notifynder_config()->isPolymorphic()) {
87
            return $this->morphTo('to');
88
        }
89
90
        return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'to_id');
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getCustomFillableFields()
97
    {
98
        return notifynder_config()->getAdditionalFields();
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    protected function mergeFillables()
105
    {
106
        $fillables = array_unique($this->getFillable() + $this->getCustomFillableFields());
107
108
        return $fillables;
109
    }
110
111
    /**
112
     * @return string
113
     * @throws \Fenos\Notifynder\Exceptions\ExtraParamsException
114
     */
115
    public function getTextAttribute()
116
    {
117
        if (! array_key_exists('text', $this->attributes)) {
118
            $notifynderParse = new NotificationParser();
119
            $this->attributes['text'] = $notifynderParse->parse($this, $this->category_id);
0 ignored issues
show
Documentation introduced by
The property category_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...
120
        }
121
122
        return $this->attributes['text'];
123
    }
124
125
    /**
126
     * @return bool|int
127
     */
128
    public function read()
129
    {
130
        return $this->update(['read' => 1]);
131
    }
132
133
    /**
134
     * @return bool|int
135
     */
136
    public function unread()
137
    {
138
        return $this->update(['read' => 0]);
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    public function resend()
145
    {
146
        $this->updateTimestamps();
147
        $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...
148
149
        return $this->save();
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    public function isAnonymous()
156
    {
157
        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...
158
    }
159
160
    /**
161
     * @param Builder $query
162
     * @param $category
163
     * @return Builder
164
     */
165
    public function scopeByCategory(Builder $query, $category)
166
    {
167
        $categoryId = NotificationCategory::getIdByCategory($category);
168
169
        return $query->where('category_id', $categoryId);
170
    }
171
172
    /**
173
     * @param Builder $query
174
     * @param int $read
175
     * @return Builder
176
     */
177
    public function scopeByRead(Builder $query, $read = 1)
178
    {
179
        return $query->where('read', $read);
180
    }
181
}
182