Completed
Pull Request — master (#13)
by ARCANEDEV
03:34
created

Participation::participable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\LaravelMessenger\Models;
2
3
use Arcanedev\LaravelMessenger\Contracts\Participation as ParticipantContract;
4
use Illuminate\Database\Eloquent\SoftDeletes;
5
6
/**
7
 * Class     Participant
8
 *
9
 * @package  Arcanedev\LaravelMessenger\Models
10
 * @author   ARCANEDEV <[email protected]>
11
 *
12
 * @property  int                                            id
13
 * @property  int                                            discussion_id
14
 * @property  \Arcanedev\LaravelMessenger\Models\Discussion  discussion
15
 * @property  string                                         participable_type
16
 * @property  int                                            participable_id
17
 * @property  \Illuminate\Database\Eloquent\Model            participable
18
 * @property  \Carbon\Carbon                                 last_read
19
 * @property  \Carbon\Carbon                                 created_at
20
 * @property  \Carbon\Carbon                                 updated_at
21
 * @property  \Carbon\Carbon                                 deleted_at
22
 */
23
class Participation extends Model implements ParticipantContract
24
{
25
    /* -----------------------------------------------------------------
26
     |  Traits
27
     | -----------------------------------------------------------------
28
     */
29
30
    use SoftDeletes;
31
32
    /* -----------------------------------------------------------------
33
     |  Properties
34
     | -----------------------------------------------------------------
35
     */
36
37
    /**
38
     * The attributes that can be set with Mass Assignment.
39
     *
40
     * @var array
41
     */
42
    protected $fillable = ['discussion_id', 'participable_type', 'participable_id', 'last_read'];
43
44
    /**
45
     * The attributes that should be mutated to dates.
46
     *
47
     * @var array
48
     */
49
    protected $dates = ['last_read', 'deleted_at'];
50
51
    /**
52
     * The attributes that should be cast to native types.
53
     *
54
     * @var array
55
     */
56
    protected $casts = [
57
        'id'              => 'integer',
58
        'discussion_id'   => 'integer',
59
        'participable_id' => 'integer',
60
    ];
61
62
    /* -----------------------------------------------------------------
63
     |  Constructor
64
     | -----------------------------------------------------------------
65
     */
66
67
    /**
68
     * Create a new Eloquent model instance.
69
     *
70
     * @param  array  $attributes
71
     */
72 93
    public function __construct(array $attributes = [])
73
    {
74 93
        $this->setTable(
75 93
            $this->getTableFromConfig('participations', 'participations')
76
        );
77
78 93
        parent::__construct($attributes);
79 93
    }
80
81
    /* -----------------------------------------------------------------
82
     |  Relationships
83
     | -----------------------------------------------------------------
84
     */
85
86
    /**
87
     * Discussion relationship.
88
     *
89
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
90
     */
91 3
    public function discussion()
92
    {
93 3
        return $this->belongsTo(
94 3
            $this->getModelFromConfig('discussions', Discussion::class)
95
        );
96
    }
97
98
    /**
99
     * Participable relationship.
100
     *
101
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
102
     */
103 27
    public function participable()
104
    {
105 27
        return $this->morphTo();
106
    }
107
108
    /* -----------------------------------------------------------------
109
     |  Getters & Setters
110
     | -----------------------------------------------------------------
111
     */
112
113
    /**
114
     * Get the participable string info.
115
     *
116
     * @return string
117
     */
118 3
    public function stringInfo()
119
    {
120 3
        return $this->participable->name;
121
    }
122
}
123