|
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([ |
|
|
|
|
|
|
98
|
|
|
'add_hours' => (float) $time, |
|
99
|
|
|
'deductable' => false, |
|
100
|
|
|
]); |
|
101
|
|
|
|
|
102
|
|
|
$this->refresh(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.