NotificationCategory   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A notifications() 0 6 1
A setNameAttribute() 0 8 2
A getTemplateBodyAttribute() 0 12 3
A scopeByName() 0 4 1
A getIdByCategory() 0 11 3
1
<?php
2
3
namespace Fenos\Notifynder\Models;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
9
/**
10
 * Class NotificationCategory.
11
 *
12
 * @property string $name
13
 * @property string $text
14
 * @method Builder byName($name)
15
 */
16
class NotificationCategory extends Model
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $table = 'notification_categories';
22
23
    /**
24
     * @var array
25
     */
26
    protected $fillable = [
27
        'name',
28
        'text',
29
    ];
30
31
    /**
32
     * @var array
33
     */
34
    protected $appends = [
35
        'template_body',
36
    ];
37
38
    /**
39
     * @var bool
40
     */
41
    public $timestamps = false;
42
43
    public function __construct(array $attributes = [])
44
    {
45
        $table = app('notifynder.resolver.model')->getTable(get_class($this));
46
        if (! empty($table)) {
47
            $this->setTable($table);
48
        }
49
50
        $attributes = array_merge([
51
            'text' => '',
52
        ], $attributes);
53
54
        parent::__construct($attributes);
55
    }
56
57
    /**
58
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
59
     */
60
    public function notifications()
61
    {
62
        $model = app('notifynder.resolver.model')->getModel(Notification::class);
63
64
        return $this->hasMany($model, 'category_id');
65
    }
66
67
    public function setNameAttribute($value)
68
    {
69
        $parts = explode('.', $value);
70
        foreach ($parts as $i => $part) {
71
            $parts[$i] = Str::slug(preg_replace('/[^a-z0-9_]/', '_', strtolower($part)), '_');
72
        }
73
        $this->attributes['name'] = implode('.', $parts);
74
    }
75
76
    /**
77
     * @return \Symfony\Component\Translation\TranslatorInterface|string
78
     */
79
    public function getTemplateBodyAttribute()
80
    {
81
        if (notifynder_config()->isTranslated()) {
82
            $key = notifynder_config()->getTranslationDomain().'.'.$this->name;
83
            $trans = trans($key);
84
            if ($trans != $key) {
85
                return $trans;
86
            }
87
        }
88
89
        return $this->text;
90
    }
91
92
    /**
93
     * @param Builder $query
94
     * @param $name
95
     * @return Builder
96
     */
97
    public function scopeByName(Builder $query, $name)
98
    {
99
        return $query->where('name', $name);
100
    }
101
102
    /**
103
     * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
104
     * @return int
105
     */
106
    public static function getIdByCategory($category)
107
    {
108
        $categoryId = $category;
109
        if ($category instanceof self) {
110
            $categoryId = $category->getKey();
111
        } elseif (! is_numeric($category)) {
112
            $categoryId = self::byName($category)->firstOrFail()->getKey();
0 ignored issues
show
Bug introduced by
The method getKey does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Database\Concerns\BuildsQueries.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
113
        }
114
115
        return $categoryId;
116
    }
117
}
118