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

NotificationCategory::getIdByCategory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 9.4285
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