Completed
Pull Request — master (#132)
by Fabrizio
13:48 queued 11:02
created

Notification::getTemplateBodyAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
3
namespace Fenos\Notifynder\Models;
4
5
use Fenos\Notifynder\Notifications\ExtraParams;
6
use Fenos\Notifynder\Parsers\NotifynderParser;
7
use Illuminate\Contracts\Container\Container;
8
use Illuminate\Database\Eloquent\Model;
9
use Carbon\Carbon;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Class Notification.
14
 *
15
 * @property int to_id
16
 * @property string to_type
17
 * @property int from_id
18
 * @property string from_type
19
 * @property int category_id
20
 * @property int read
21
 * @property string url
22
 * @property string extra
23
 *
24
 * Php spec complain when model is mocked
25
 * if I turn them on as php doc block
26
 *
27
 * @method wherePolymorphic
28
 * @method withNotRead
29
 */
30
class Notification extends Model
31
{
32
    /**
33
     * @var array
34
     */
35
    protected $fillable = [
36
        'to_id', 'to_type', 'from_id', 'from_type',
37
        'category_id', 'read', 'url', 'extra', 'expire_time',
38
    ];
39
    protected $apopends = [
40
        'template_body',
41
        'notify_body',
42
    ];
43
44
    /**
45
     * Notification constructor.
46
     *
47
     * @param array $attributes
48
     */
49
    public function __construct(array $attributes = [])
50
    {
51
        $fillables = $this->mergeFillable();
52
        $this->fillable($fillables);
53
54
        parent::__construct($attributes);
55
    }
56
57
    /**
58
     * Custom Collection.
59
     *
60
     * @param  array                                                         $models
61
     * @return NotifynderCollection|\Illuminate\Database\Eloquent\Collection
62
     */
63
    public function newCollection(array $models = [])
64
    {
65
        return new NotifynderCollection($models);
66
    }
67
68
    /**
69
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
70
     */
71
    public function body()
72
    {
73
        return $this->belongsTo(NotificationCategory::class, 'category_id');
74
    }
75
76
    /**
77
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
78
     */
79 View Code Duplication
    public function from()
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...
80
    {
81
        // check if on the configurations file there is the option
82
        // polymorphic setted to true, if so Notifynder will work
83
        // polymorphic.
84
        if (config('notifynder.polymorphic') == false) {
85
            return $this->belongsTo(config('notifynder.model'), 'from_id');
86
        } else {
87
            return $this->morphTo();
88
        }
89
    }
90
91
    /**
92
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
93
     */
94 View Code Duplication
    public function to()
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...
95
    {
96
        // check if on the configurations file there is the option
97
        // polymorphic setted to true, if so Notifynder will work
98
        // polymorphic.
99
        if (config('notifynder.polymorphic') == false) {
100
            return $this->belongsTo(config('notifynder.model'), 'to_id');
101
        } else {
102
            return $this->morphTo();
103
        }
104
    }
105
106
    /**
107
     * Not read scope.
108
     *
109
     * @param $query
110
     * @return mixed
111
     */
112
    public function scopeWithNotRead($query)
113
    {
114
        return $query->where('read', 0);
115
    }
116
117
    /**
118
     * Only Expired Notification scope.
119
     *
120
     * @param $query
121
     * @return mixed
122
     */
123
    public function scopeOnlyExpired($query)
124
    {
125
        return $query->where('expire_time', '<', Carbon::now());
126
    }
127
128
    /**
129
     * Where Polymorphic.
130
     *
131
     * @param $query
132
     * @param $id
133
     * @param $type
134
     * @return mixed
135
     */
136
    public function scopeWherePolymorphic($query, $id, $type)
137
    {
138
        if (! $type or config('notifynder.polymorphic') === false) {
139
            return $query->where('to_id', $id);
140
        } else {
141
            return $query->where('to_id', $id)
142
                ->where('to_type', $type);
143
        }
144
    }
145
146
    public function getTemplateBodyAttribute()
147
    {
148
        if (config('notifynder.translation.enabled', false)) {
149
            $key = config('notifynder.translation.domain', 'notifynder').'.'.$this->body->name;
0 ignored issues
show
Documentation introduced by
The property body 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...
150
            $trans = trans($key);
151
            if ($trans != $key) {
152
                return $trans;
153
            }
154
        }
155
156
        return $this->body->text;
0 ignored issues
show
Documentation introduced by
The property body 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...
157
    }
158
159
    /**
160
     * Get parsed body attributes.
161
     *
162
     * @return string
163
     */
164
    public function getNotifyBodyAttribute()
165
    {
166
        $notifynderParse = new NotifynderParser();
167
168
        return $notifynderParse->parse($this);
169
    }
170
171
    /**
172
     * @param $value
173
     * @return \Fenos\Notifynder\Notifications\ExtraParams
174
     */
175
    public function getExtraAttribute($value)
176
    {
177
        if (! empty($value)) {
178
            return new ExtraParams($value);
179
        }
180
181
        return new ExtraParams([]);
182
    }
183
184
    /**
185
     * Filter Scope by category.
186
     *
187
     * @param $query
188
     * @param $category
189
     * @return mixed
190
     */
191
    public function scopeByCategory($query, $category)
192
    {
193
        if (is_numeric($category)) {
194
            return $query->where('category_id', $category);
195
        }
196
197
        return $query->whereHas('body', function ($categoryQuery) use ($category) {
198
            $categoryQuery->where('name', $category);
199
        });
200
    }
201
202
    /**
203
     * Get custom required fields from the configs
204
     * so that we can automatically bind them to the model
205
     * fillable property.
206
     *
207
     * @return mixed
208
     */
209
    public function getCustomFillableFields()
210
    {
211
        if (function_exists('app') && app() instanceof Container) {
212
            return Arr::flatten(config('notifynder.additional_fields', []));
213
        }
214
215
        return [];
216
    }
217
218
    /**
219
     * @return array
220
     */
221
    protected function mergeFillable()
222
    {
223
        $fillables = array_unique($this->getFillable() + $this->getCustomFillableFields());
224
225
        return $fillables;
226
    }
227
}
228