Completed
Push — version-4 ( 9e921c...18159e )
by
unknown
05:03
created

NotificationCategory::getTemplateBodyAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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 array
29
     */
30
    protected $appends = [
31
        'template_body',
32
    ];
33
34
    /**
35
     * @var bool
36
     */
37
    public $timestamps = false;
38
39
    public function __construct(array $attributes = [])
40
    {
41
        $attributes = array_merge([
42
            'text' => '',
43
        ], $attributes);
44
45
        parent::__construct($attributes);
46
    }
47
48
    /**
49
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
50
     */
51
    public function notifications()
52
    {
53
        $config = app('notifynder.config');
54
        $model = $config->getNotificationModel();
55
56
        return $this->hasMany($model, 'category_id');
57
    }
58
59
    public function setNameAttribute($value)
60
    {
61
        $parts = explode('.', $value);
62
        foreach ($parts as $i => $part) {
63
            $parts[$i] = Str::slug(preg_replace('/[^a-z0-9_]/', '_', strtolower($part)), '_');
64
        }
65
        $this->attributes['name'] = implode('.', $parts);
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getTemplateBodyAttribute()
72
    {
73
        if (notifynder_config()->isTranslated()) {
74
            $key = notifynder_config()->getTranslationDomain().'.'.$this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Fenos\Notifynder\...s\NotificationCategory>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
75
            $trans = trans($key);
0 ignored issues
show
Bug Compatibility introduced by
The expression trans($key); of type Symfony\Component\Transl...nslatorInterface|string adds the type Symfony\Component\Translation\TranslatorInterface to the return on line 77 which is incompatible with the return type documented by Fenos\Notifynder\Models\...etTemplateBodyAttribute of type string.
Loading history...
76
            if ($trans != $key) {
77
                return $trans;
78
            }
79
        }
80
81
        return $this->text;
0 ignored issues
show
Documentation introduced by
The property text does not exist on object<Fenos\Notifynder\...s\NotificationCategory>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
82
    }
83
84
    /**
85
     * @param Builder $query
86
     * @param $name
87
     * @return Builder
88
     */
89
    public function scopeByName(Builder $query, $name)
90
    {
91
        return $query->where('name', $name);
92
    }
93
94
    /**
95
     * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
96
     * @return int
97
     */
98
    public static function getIdByCategory($category)
99
    {
100
        $categoryId = $category;
101
        if ($category instanceof self) {
102
            $categoryId = $category->getKey();
103
        } elseif (! is_numeric($category)) {
104
            $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...
105
        }
106
107
        return $categoryId;
108
    }
109
}
110