Completed
Push — version-4 ( 9f3bb5...94cdcc )
by
unknown
02:54
created

Notification::getCustomFillableFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
cc 1
eloc 2
c 4
b 1
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\Model;
8
9
class Notification extends Model
10
{
11
    protected $fillable = [
12
        'to_id',
13
        'to_type',
14
        'from_id',
15
        'from_type',
16
        'category_id',
17
        'read',
18
        'url',
19
        'extra',
20
        'expire_time', // ToDo: rename to `expires_at`
21
        'stack_id',
22
    ];
23
24
    protected $appends = [
25
        'text',
26
    ];
27
28
    protected $casts = [
29
        'extra' => 'array',
30
    ];
31
32
    public function __construct($attributes = [])
33
    {
34
        $this->fillable($this->mergeFillables());
35
36
        if ($attributes instanceof BuilderNotification) {
37
            $attributes = $attributes->toArray();
38
        }
39
40
        parent::__construct($attributes);
41
    }
42
43
    public function category()
44
    {
45
        return $this->belongsTo(NotificationCategory::class, 'category_id');
46
    }
47
48
    public function from()
49
    {
50
        if (notifynder_config()->isPolymorphic()) {
51
            return $this->belongsTo(notifynder_config()->getModel(), 'from_id');
52
        }
53
54
        return $this->morphTo();
55
    }
56
57
    public function to()
58
    {
59
        if (notifynder_config()->isPolymorphic()) {
60
            return $this->belongsTo(notifynder_config()->getModel(), 'to_id');
61
        }
62
63
        return $this->morphTo();
64
    }
65
66
    public function getCustomFillableFields()
67
    {
68
        return notifynder_config()->getAdditionalFields();
69
    }
70
71
    protected function mergeFillables()
72
    {
73
        $fillables = array_unique($this->getFillable() + $this->getCustomFillableFields());
74
75
        return $fillables;
76
    }
77
78
    public function getTemplateBodyAttribute()
79
    {
80
        if (notifynder_config()->isTranslated()) {
81
            $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...
82
            $trans = trans($key);
83
            if ($trans != $key) {
84
                return $trans;
85
            }
86
        }
87
        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...
88
    }
89
90
    public function getTextAttribute()
91
    {
92
        if(!array_key_exists('text', $this->attributes)) {
93
            $notifynderParse = new NotificationParser();
94
            $this->attributes['text'] = $notifynderParse->parse($this);
95
        }
96
        return $this->attributes['text'];
97
    }
98
}
99