Completed
Push — version-4 ( 3a8e98...bdf5b5 )
by
unknown
05:06
created

NotificationCategory::categories()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A NotificationCategory::notifications() 0 7 1
1
<?php
2
3
namespace Fenos\Notifynder\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Str;
8
9
/**
10
 * Class NotificationCategory.
11
 */
12
class NotificationCategory extends Model
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $table = 'notification_categories';
18
19
    /**
20
     * @var array
21
     */
22
    protected $fillable = [
23
        'name',
24
        'text',
25
    ];
26
27
    /**
28
     * @var bool
29
     */
30
    public $timestamps = false;
31
32
    public function __construct(array $attributes)
33
    {
34
        $attributes = array_merge([
35
            'text' => '',
36
        ], $attributes);
37
38
        parent::__construct($attributes);
39
    }
40
41
    /**
42
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
43
     */
44
    public function notifications()
45
    {
46
        $config = app('notifynder.config');
47
        $model = $config->getNotificationModel();
48
49
        return $this->hasMany($model, 'category_id');
50
    }
51
52
    public function setNameAttribute($value)
53
    {
54
        $parts = explode('.', $value);
55
        foreach($parts as $i => $part) {
56
            $parts[$i] = Str::slug(preg_replace('/[^a-z0-9_]/', '_', strtolower($part)), '_');
57
        }
58
        $this->attributes['name'] = implode('.', $parts);
59
    }
60
61
    /**
62
     * @param Builder $query
63
     * @param $name
64
     * @return Builder
65
     */
66
    public function scopeByName(Builder $query, $name)
67
    {
68
        return $query->where('name', $name);
69
    }
70
71
    /**
72
     * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
73
     * @return int
74
     */
75
    public static function getIdByCategory($category)
76
    {
77
        $categoryId = $category;
78
        if ($category instanceof self) {
79
            $categoryId = $category->getKey();
80
        } elseif (! is_numeric($category)) {
81
            $categoryId = self::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...
82
        }
83
84
        return $categoryId;
85
    }
86
}
87