Completed
Push — version-4 ( f39f28...cf59bd )
by
unknown
07:07
created

NotificationCategory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 6
Bugs 2 Features 2
Metric Value
c 6
b 2
f 2
dl 0
loc 67
rs 10
wmc 6
lcom 0
cbo 2

4 Methods

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