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

Project::tasks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Chriscreates\Projects\Models;
4
5
use BadMethodCallException;
6
use Carbon\Carbon;
7
use Chriscreates\Projects\Traits\HasStatus;
8
use Chriscreates\Projects\Traits\HasUsers;
9
use Chriscreates\Projects\Traits\IsMeasurable;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Database\Eloquent\Relations\HasOne;
12
use Illuminate\Database\Eloquent\Relations\MorphToMany;
13
14
class Project extends Model
15
{
16
    use IsMeasurable, HasUsers, HasStatus;
17
18
    protected $table = 'projects';
19
20
    public $primaryKey = 'id';
21
22
    public $guarded = ['id'];
23
24
    public $timestamps = true;
25
26
    private $user_model;
27
28
    protected $dates = [
29
        'started_at',
30
        'delivered_at',
31
        'expected_at',
32
    ];
33
34
    protected $casts = [
35
        'visible' => 'boolean',
36
    ];
37
38
    public function __construct(array $attributes = [])
39
    {
40
        $this->user_model = config('projects.user_class');
41
42
        parent::__construct($attributes);
43
    }
44
45
    public function tasks() : MorphToMany
46
    {
47
        return $this->morphedByMany(Task::class, 'projectable');
48
    }
49
50
    public function status() : HasOne
51
    {
52
        return $this->hasOne(Status::class, 'id', 'status_id');
53
    }
54
55
    public function isVisible() : bool
56
    {
57
        return $this->attributes['visible'] == true;
58
    }
59
60
    public function isNotVisible() : bool
61
    {
62
        return $this->attributes['visible'] == false;
63
    }
64
65
    public function dueIn(string $format = 'days') : int
66
    {
67
        $method = 'diffIn'.ucfirst($format);
68
69
        if ( ! $this->hasDateTarget()) {
70
            return 0;
71
        }
72
73
        if ( ! method_exists(Carbon::class, $method)) {
74
            throw new BadMethodCallException(sprintf(
75
                'Method %s::%s does not exist.',
76
                Carbon::class,
77
                $method
78
            ));
79
        }
80
81
        return $this->started_at->$method($this->expected_at);
82
    }
83
}
84