Completed
Push — master ( 02dfbd...2340d0 )
by Christopher
01:13
created

Task::recordTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Chriscreates\Projects\Models;
4
5
use Chriscreates\Projects\Traits\HasPriority;
6
use Chriscreates\Projects\Traits\IsRecordable;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Eloquent\Relations\MorphMany;
10
use Illuminate\Database\Eloquent\Relations\MorphToMany;
11
12
class Task extends Model
13
{
14
    use IsRecordable, HasPriority;
15
16
    protected $table = 'tasks';
17
18
    public $primaryKey = 'id';
19
20
    public $guarded = ['id'];
21
22
    public $timestamps = true;
23
24
    protected $casts = [
25
        'complete' => 'bool',
26
        'deduct_hours' => 'integer',
27
    ];
28
29
    public function creator() : BelongsTo
30
    {
31
        return $this->belongsTo(get_class(user_model()));
32
    }
33
34
    public function priority() : BelongsTo
35
    {
36
        return $this->belongsTo(Priority::class);
37
    }
38
39
    public function projects() : MorphToMany
40
    {
41
        return $this->morphToMany(Project::class, 'projectable');
42
    }
43
44
    public function records() : MorphMany
45
    {
46
        return $this->morphMany(Record::class, 'recordable');
47
    }
48
49
    public function assignToProject(Project $project) : void
50
    {
51
        $this->projects()->save($project);
52
53
        $this->refresh();
54
    }
55
56
    public function markCompletion(bool $bool) : void
57
    {
58
        $this->update(['complete' => $bool]);
59
    }
60
61
    public function completed() : bool
62
    {
63
        return $this->attributes['complete'];
64
    }
65
66
    public function recordTime($from, $to) : void
67
    {
68
        $this->records()->create([
69
            'time_from' => $from,
70
            'time_to' => $to,
71
        ]);
72
73
        $this->refresh();
74
    }
75
76
    public function removeRecord(Record $record) : void
77
    {
78
        if ($record = $this->records->firstWhere('id', $record->id)) {
79
            $record->delete();
80
        }
81
82
        $this->refresh();
83
    }
84
85
    public function deductHours($time) : void
86
    {
87
        $this->records()->create([
88
            'deduct_hours' => (float) $time,
89
            'deductable' => true,
90
        ]);
91
92
        $this->refresh();
93
    }
94
95
    public function addHours($time) : void
96
    {
97
        $record = $this->records()->create([
0 ignored issues
show
Unused Code introduced by
$record is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
            'add_hours' => (float) $time,
99
            'deductable' => false,
100
        ]);
101
102
        $this->refresh();
103
    }
104
}
105