Completed
Push — master ( 053977...560004 )
by Christopher
03:12
created

Record::hours()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Chriscreates\Projects\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\MorphTo;
8
use Illuminate\Support\Facades\DB;
9
10
class Record extends Model
11
{
12
    protected $table = 'records';
13
14
    public $primaryKey = 'id';
15
16
    public $guarded = ['id'];
17
18
    public $timestamps = true;
19
20
    protected $dates = [
21
        'time_from',
22
        'time_to',
23
    ];
24
25
    protected $appends = [
26
        'hours',
27
    ];
28
29
    /**
30
     * The "booted" method of the model.
31
     *
32
     * @return void
33
     */
34
    protected static function booted()
35
    {
36
        parent::boot();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (boot() instead of booted()). Are you sure this is correct? If so, you might want to change this to $this->boot().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
37
38
        static::addGlobalScope('hours', function (Builder $builder) {
39
            $builder->select('*');
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

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...
40
            $builder->addSelect(
41
                DB::raw('ROUND(TIMESTAMPDIFF(MINUTE, `time_from`, `time_to`)/60, 2) as `hours`')
42
            );
43
        });
44
    }
45
46
    public function recordable() : MorphTo
47
    {
48
        return $this->morphTo();
49
    }
50
51
    public function getHoursAttribute() : float
52
    {
53
        if (is_null($this->time_from) || is_null($this->time_to)) {
54
            return 0;
55
        }
56
57
        $hours = $this->time_from->floatDiffInRealHours($this->time_to, false);
58
59
        return $hours;
60
    }
61
62
    public function hours($rounded_up = false)
63
    {
64
        if ( ! isset($this->attributes['hours']) || empty($this->attributes['hours'])) {
65
            return 0;
66
        }
67
68
        if ($rounded_up) {
69
            return round($this->attributes['hours'] * 2) / 2;
70
        }
71
72
        return $this->attributes['hours'];
73
    }
74
}
75