Completed
Push — issue-37 ( e24320...44d7c4 )
by Fèvre
03:48
created

Experience   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 76
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 11 2
A sumCaches() 0 6 1
A user() 0 4 1
A obtainable() 0 4 1
1
<?php
2
namespace Xetaravel\Models;
3
4
use Eloquence\Behaviours\SumCache\Summable;
5
use Illuminate\Support\Facades\Auth;
6
7
class Experience extends Model
8
{
9
    use Summable;
10
11
    /**
12
     * The attributes that are mass assignable.
13
     *
14
     * @var array
15
     */
16
    protected $fillable = [
17
        'user_id',
18
        'amount',
19
        'obtainable_id',
20
        'obtainable_type',
21
        'event_type',
22
        'data'
23
    ];
24
25
    /**
26
     * The attributes that should be cast to native types.
27
     *
28
     * @var array
29
     */
30
    protected $casts = [
31
        'data' => 'array'
32
    ];
33
34
    /**
35
     * The "booting" method of the model.
36
     *
37
     * @return void
38
     */
39
    protected static function boot()
40
    {
41
        parent::boot();
42
43
        // Set the user id to the new log before saving it.
44
        static::creating(function ($model) {
45
            if (is_null($model->user_id)) {
46
                $model->user_id = Auth::id();
47
            }
48
        });
49
    }
50
51
    /**
52
     * Return the sum cache configuration.
53
     *
54
     * @return array
55
     */
56
    public function sumCaches()
57
    {
58
        return [
59
            'experiences_total' => [User::class, 'amount', 'user_id', 'id']
60
        ];
61
    }
62
63
    /**
64
     * Get the user that owns the log.
65
     *
66
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
67
     */
68
    public function user()
69
    {
70
        return $this->belongsTo(User::class);
71
    }
72
73
    /**
74
     * Get the obtainable relation.
75
     *
76
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
77
     */
78
    public function obtainable()
79
    {
80
        return $this->morphTo();
81
    }
82
}
83