Completed
Push — develop ( 31039e...ea14db )
by Mohamed
05:36
created

Issue::getCountCommentsAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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\Project;
13
14
use Illuminate\Database\Eloquent\Model as BaseModel;
15
use Illuminate\Support\Collection;
16
use Tinyissue\Contracts\Model\AccessControl;
17
use Tinyissue\Extensions\Auth\LoggedUser;
18
use Tinyissue\Model;
19
use Tinyissue\Model\Traits\CountAttributeTrait;
20
use Tinyissue\Model\Traits\Project\Issue\CountTrait;
21
use Tinyissue\Model\Traits\Project\Issue\CrudTagTrait;
22
use Tinyissue\Model\Traits\Project\Issue\CrudTrait;
23
use Tinyissue\Model\Traits\Project\Issue\QueryTrait;
24
use Tinyissue\Model\Traits\Project\Issue\QueueTrait;
25
use Tinyissue\Model\Traits\Project\Issue\RelationTrait;
26
27
/**
28
 * Issue is model class for project issues.
29
 *
30
 * @author Mohamed Alsharaf <[email protected]>
31
 *
32
 * @property int $id
33
 * @property int $created_by
34
 * @property int $project_id
35
 * @property string $title
36
 * @property string $body
37
 * @property int $assigned_to
38
 * @property int $time_quote
39
 * @property bool $lock_quote
40
 * @property int $closed_by
41
 * @property int $closed_at
42
 * @property int status
43
 * @property int $updated_at
44
 * @property int $updated_by
45
 * @property Model\Project $project
46
 * @property Model\User $user
47
 * @property Model\User $assigned
48
 * @property Model\User $closers
49
 * @property Model\User $updatedBy
50
 * @property Collection $attachments
51
 * @property Collection $activities
52
 * @property Collection $generalActivities
53
 * @property Collection $commentActivities
54
 * @property Collection $tags
55
 * @property Collection $comments
56
 * @property Collection $messagesQueue
57
 */
58
class Issue extends BaseModel implements AccessControl
59
{
60
    use CountAttributeTrait,
61
        CountTrait,
62
        CrudTrait,
63
        CrudTagTrait,
64
        RelationTrait,
65
        QueryTrait,
66
        QueueTrait,
67
        LoggedUser;
68
69
    /**
70
     * Issue status: Open.
71
     *
72
     * @var int
73
     */
74
    const STATUS_OPEN = 1;
75
76
    /**
77
     * Issue status: Closed.
78
     *
79
     * @var int
80
     */
81
    const STATUS_CLOSED = 0;
82
83
    /**
84
     * Timestamp enabled.
85
     *
86
     * @var bool
87
     */
88
    public $timestamps = true;
89
90
    /**
91
     * Name of database table.
92
     *
93
     * @var string
94
     */
95
    protected $table = 'projects_issues';
96
97
    /**
98
     * List of allowed columns to be used in $this->fill().
99
     *
100
     * @var array
101
     */
102
    protected $fillable = ['created_by', 'project_id', 'title', 'body', 'assigned_to', 'time_quote', 'lock_quote'];
103
104
    /**
105
     * Set attributes default value.
106
     *
107
     * @var array
108
     */
109
    protected $attributes = [
110
        'status' => self::STATUS_OPEN,
111
    ];
112
113
    /**
114
     * Returns the aggregate value of number of comments in an issue.
115 6
     *
116
     * @return int
117 6
     */
118
    public function getCountCommentsAttribute()
119
    {
120
        return $this->getCountAttribute('countComments');
121
    }
122
123
    /**
124
     * Generate a URL for the active project.
125
     *
126
     * @param string $url
127 30
     *
128
     * @return string
129 30
     */
130
    public function to($url = '')
131
    {
132
        return \URL::to('project/' . $this->project_id . '/issue/' . $this->id . (($url) ? '/' . $url : ''));
133
    }
134
135
    /**
136
     * Convert time quote from an array into seconds.
137 36
     *
138
     * @param array $value
139 36
     */
140 36
    public function setTimeQuoteAttribute($value)
141 35
    {
142 35
        $seconds = $value;
143 35
        if (is_array($value)) {
144
            $seconds = 0;
145 36
            $seconds += isset($value['m']) ? ($value['m'] * 60) : 0;
146 36
            $seconds += isset($value['h']) ? ($value['h'] * 60 * 60) : 0;
147
        }
148
        $this->attributes['time_quote'] = (int) $seconds;
149
    }
150
151
    /**
152
     * Returns the color of tag status.
153
     *
154
     * @return string
155 1
     */
156 1
    public function getTypeColorAttribute()
157 1
    {
158
        $tag = $this->tags->filter(function (Model\Tag $tag) {
159 1
            return $tag->parent->name === 'type';
160
        })->first();
161
162
        if ($tag) {
163 1
            return $tag->bgcolor;
164
        }
165
166
        return null;
167
    }
168
169
    /**
170
     * Whether or not the issue is new.
171
     *
172
     * @return bool
173
     */
174
    public function isNew()
175
    {
176
        if ($this->status === 0) {
177
            return false;
178
        }
179
180
        return $this->tags->count() === 0;
181
    }
182
183
    /**
184
     * Whether or not the issue is open or closed.
185 27
     *
186
     * @return bool
187 27
     */
188
    public function isOpen()
189
    {
190
        return (boolean) $this->status;
191
    }
192
193
    /**
194
     * Check if the issue contains a tag with option to set the issue as readonly to current user.
195
     *
196
     * @param Model\User $user
197 14
     *
198
     * @return bool
199 14
     */
200
    public function hasReadOnlyTag(Model\User $user)
201 14
    {
202
        $hasReadOnly = $this->tags->where('readonly', $user->role_id, false);
203
204
        return !$hasReadOnly->isEmpty();
205
    }
206
207
    /**
208
     * Whether or not the issue quote is locked by manager.
209 11
     *
210
     * @return bool
211 11
     */
212
    public function isQuoteLocked()
213
    {
214
        return (boolean) $this->lock_quote;
215
    }
216
217
    /**
218
     * Check if a user is allowed to see the issue quote.
219
     *
220
     * @param Model\User $user
221 23
     *
222
     * @return bool
223 23
     */
224 23
    public function canUserViewQuote(Model\User $user = null)
225
    {
226 5
        if ($user && $this->time_quote > 0 &&
227
            (!$this->isQuoteLocked() || $user->permission(Model\Permission::PERM_ISSUE_VIEW_LOCKED_QUOTE))
228
        ) {
229 23
            return true;
230
        }
231
232
        return false;
233
    }
234
235
    /**
236
     * Whether or not a user is the creator of the issue.
237
     *
238
     * @param Model\User $user
239 17
     *
240
     * @return bool
241 17
     */
242
    public function isCreatedBy(Model\User $user)
243
    {
244
        return $this->created_by === $user->id;
245
    }
246
247
    /**
248
     * Whether a user can view the issue.
249
     *
250
     * @param Model\User $user
251 9
     *
252
     * @return bool
253
     */
254
    public function canView(Model\User $user)
255 9
    {
256 9
        // not access if issue limited to developers and managers, or user is not member of the project
257
        if (
258 2
            ($this->project->isPrivateInternal() && $user->isUser() && !$this->isCreatedBy($user)) ||
259
            !$this->project->isMember($user->id)
260
        ) {
261 9
            return false;
262
        }
263
264
        return true;
265
    }
266
267
    /**
268
     * Whether a user can edit the issue.
269
     *
270
     * @param Model\User $user
271 17
     *
272
     * @return bool
273
     */
274 17
    public function canEdit(Model\User $user)
275
    {
276
        // If you have permission to modify issue or a creator and current tag is not read only.
277
        return ($this->isCreatedBy($user) && !$this->hasReadOnlyTag($user)) || ($this->canView($user) && $user->permission(Model\Permission::PERM_ISSUE_MODIFY));
278
    }
279
280
    /**
281
     * @param string     $permission
282
     * @param Model\User $user
283
     *
284
     * @return bool
285
     */
286 View Code Duplication
    public function can($permission, Model\User $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
287
    {
288
        $editPermissions = [
289
            Model\Permission::PERM_ISSUE_COMMENT,
290
            Model\Permission::PERM_ISSUE_MODIFY,
291
            Model\Permission::PERM_ISSUE_LOCK_QUOTE,
292
        ];
293
294
        if (in_array($permission, $editPermissions)) {
295
            return $this->canEdit($user);
296
        }
297
298
        return $this->canView($user);
299
    }
300
}
301