TrainingUser::setAttribute()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 12
1
<?php
2
3
namespace SET;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class TrainingUser extends Model
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $table = 'training_user';
13
    /**
14
     * @var bool
15
     */
16
    public $timestamps = true;
17
18
    /**
19
     * @var array
20
     */
21
    protected $fillable = ['training_id', 'due_date', 'completed_date', 'comment', 'author_id', 'user_id', 'stop_renewal'];
22
23
    public function user()
24
    {
25
        return $this->belongsTo('SET\User');
26
    }
27
28
    public function author()
29
    {
30
        return $this->belongsTo('SET\User', 'author_id');
31
    }
32
33
    public function training()
34
    {
35
        return $this->belongsTo('SET\Training');
36
    }
37
38
    public function attachments()
39
    {
40
        return $this->morphMany('SET\Attachment', 'imageable');
41
    }
42
43
    /**
44
     * Allows you to call activeUsers() on notes
45
     * to return only notes with active users.
46
     *
47
     * @param $query
48
     *
49
     * @return mixed
50
     */
51
    public function scopeActiveUsers($query)
52
    {
53
        return $query->whereHas('user', function ($q) {
54
            $q->where('status', 'active');
55
        });
56
    }
57
58
    /**
59
     * @param string $key
60
     * @param mixed  $value
61
     *
62
     * @return $this
63
     */
64 View Code Duplication
    public function setAttribute($key, $value)
65
    {
66
        if (is_scalar($value) && $value === '') {
67
            $value = null;
68
        }
69
70
        return parent::setAttribute($key, $value);
71
    }
72
73
    /**
74
     * @param $query
75
     * @param $input
76
     */
77
    public function scopeRenewableTrainings($query)
78
    {
79
        return $query->whereHas('training', function ($q) {
80
            $q->where('renews_in', '>', 0);
81
        });
82
    }
83
}
84