Completed
Push — issue-105 ( c61f68...86b941 )
by Fabrizio
02:19
created

Notification::getCustomFillableFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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