Completed
Push — master ( df45e0...e7ba61 )
by Ariel
07:53
created

Service::setDurationAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Timegridio\Concierge\Models;
4
5
use Illuminate\Database\Eloquent\Model as EloquentModel;
6
7
class Service extends EloquentModel
8
{
9
    /**
10
     * The attributes that are mass assignable.
11
     *
12
     * @var array
13
     */
14
    protected $fillable = ['name', 'business_id', 'description', 'prerequisites', 'duration', 'type_id', 'color'];
15
16
    /**
17
     * The attributes that aren't mass assignable.
18
     *
19
     * @var array
20
     */
21
    protected $guarded = ['id', 'slug'];
22
23
    /**
24
     * Belongs to service type.
25
     *
26
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo Relationship Service belongs to type query
27
     */
28 3
    public function type()
29
    {
30 3
        return $this->belongsTo(ServiceType::class);
31
    }
32
33
    /**
34
     * Get service type name.
35
     *
36
     * @return string
37
     */
38 2
    public function getTypeNameAttribute()
39
    {
40 2
        if ($this->type) {
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<Timegridio\Concierge\Models\Service>. 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...
41 1
            return $this->type->name;
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<Timegridio\Concierge\Models\Service>. 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...
42
        }
43
44 1
        return '';
45
    }
46
47
    /**
48
     * belongs to Business.
49
     *
50
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo Relationship Service belongs to Business query
51
     */
52 3
    public function business()
53
    {
54 3
        return $this->belongsTo(Business::class);
55
    }
56
57
    /**
58
     * TODO: Check slug setting can be moved to a more proper place.
59
     *
60
     * Save the model to the database.
61
     *
62
     * @param array $options
63
     *
64
     * @return bool
65
     */
66 38
    public function save(array $options = [])
67
    {
68 38
        $this->attributes['slug'] = str_slug($this->attributes['name']);
69
70 38
        return parent::save($options);
71
    }
72
73
    //////////////
74
    // Mutators //
75
    //////////////
76
77
    /**
78
     * set Duration Attribute.
79
     *
80
     * @param int $duration
81
     */
82 38
    public function setDurationAttribute($duration)
83
    {
84 38
        return $this->attributes['duration'] = $duration ? intval($duration) : null;
85
    }
86
87
    ////////////
88
    // Scopes //
89
    ////////////
90
91
    /**
92
     * Scope Slug.
93
     *
94
     * @param Illuminate\Database\Query $query
95
     * @param string                    $slug  Slug of the desired Service
96
     *
97
     * @return Illuminate\Database\Query Scoped query
98
     */
99 1
    public function scopeSlug($query, $slug)
100
    {
101 1
        return $query->where('slug', '=', $slug)->get();
102
    }
103
}
104