RelationTrait::comments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model\Traits\Project\Issue;
13
14
use Illuminate\Database\Eloquent\Builder;
15
use Illuminate\Database\Eloquent\Relations;
16
use Tinyissue\Model;
17
18
/**
19
 * RelationTrait is trait class containing the relationship methods for the Project\Issue model.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 *
23
 * @property static $this
24
 */
25
trait RelationTrait
26
{
27
    /**
28
     * An issue has one user assigned to (inverse relationship of User::issues).
29
     *
30
     * @return Relations\BelongsTo
31
     */
32 14
    public function assigned()
33
    {
34 14
        return $this->belongsTo('Tinyissue\Model\User', 'assigned_to');
35
    }
36
37
    /**
38
     * An issue has one user updated by (inverse relationship of User::issuesUpdatedBy).
39
     *
40
     * @return Relations\BelongsTo
41
     */
42 37
    public function updatedBy()
43
    {
44 37
        return $this->belongsTo('Tinyissue\Model\User', 'updated_by');
45
    }
46
47
    /**
48
     * An issue has one user closed it (inverse relationship of User::issuesClosedBy).
49
     *
50
     * @return Relations\BelongsTo
51
     */
52
    public function closer()
53
    {
54
        return $this->belongsTo('Tinyissue\Model\User', 'closed_by');
55
    }
56
57
    /**
58
     * An issue has one user created it (inverse relationship of User::issuesCreatedBy).
59
     *
60
     * @return Relations\BelongsTo
61
     */
62 23
    public function user()
63
    {
64 23
        return $this->belongsTo('Tinyissue\Model\User', 'created_by');
65
    }
66
67
    /**
68
     * Issue belong to a project.
69
     *
70
     * @return Relations\BelongsTo
71
     */
72 14
    public function project()
73
    {
74 14
        return $this->belongsTo('Tinyissue\Model\Project');
75
    }
76
77
    /**
78
     * Issue can have many attachments.
79
     *
80
     * @return Relations\HasMany
81
     */
82 18
    public function attachments()
83
    {
84
        return $this
85 18
            ->hasMany('Tinyissue\Model\Project\Issue\Attachment', 'issue_id')
86 18
            ->where(function (Builder $query) {
87 18
                $query->where('comment_id', '=', 0);
88 18
                $query->orWhere('comment_id', '=', null);
89 18
            });
90
    }
91
92
    /**
93
     * Issue have many users activities.
94
     *
95
     * @return Relations\HasMany
96
     */
97 37
    public function activities()
98
    {
99
        return $this
100 37
            ->hasMany('Tinyissue\Model\User\Activity', 'item_id')
101 37
            ->orderBy('created_at', 'ASC');
102
    }
103
104
    /**
105
     * Issue have many users activities (all except comments).
106
     *
107
     * @return mixed
108
     */
109 1
    public function generalActivities()
110
    {
111
        return $this
112 1
            ->hasMany('Tinyissue\Model\User\Activity', 'item_id')
113 1
            ->whereNotIn('type_id', [Model\Activity::TYPE_COMMENT])
114 1
            ->orderBy('created_at', 'ASC');
115
    }
116
117
    /**
118
     * Issue have many users activities (comments).
119
     *
120
     * @return mixed
121
     */
122 8
    public function commentActivities()
123
    {
124
        return $this
125 8
            ->hasMany('Tinyissue\Model\User\Activity', 'item_id')
126 8
            ->whereIn('type_id', [Model\Activity::TYPE_COMMENT])
127 8
            ->orderBy('created_at', 'ASC');
128
    }
129
130
    /**
131
     * Issue have many tags.
132
     *
133
     * @return Relations\BelongsToMany
134
     */
135 37
    public function tags()
136
    {
137 37
        return $this->belongsToMany('Tinyissue\Model\Tag', 'projects_issues_tags', 'issue_id', 'tag_id')
138 37
            ->join('tags as p', 'p.id', '=', 'tags.parent_id')
139 37
            ->orderBy('parent_id');
140
    }
141
142
    /**
143
     * Issue have many comments.
144
     *
145
     * @return Relations\HasMany
146
     */
147 3
    public function comments()
148
    {
149
        return $this
150 3
            ->hasMany('Tinyissue\Model\Project\Issue\Comment', 'issue_id')
151 3
            ->orderBy('created_at', 'ASC');
152
    }
153
154
    /**
155
     * Issue can have many messages queue.
156
     *
157
     * @return Relations\HasMany
158
     */
159
    public function messagesQueue()
160
    {
161
        return $this->morphMany('Tinyissue\Model\Message\Queue', 'model');
162
    }
163
164
    abstract public function belongsToMany($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null);
165
    abstract public function hasMany($related, $foreignKey = null, $localKey = null);
166
    abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null);
167
    abstract public function hasOne($related, $foreignKey = null, $localKey = null);
168
    abstract public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null);
169
}
170