Completed
Push — develop-3.0 ( 360277...bd5ff0 )
by Mohamed
06:52
created

Issue::canUserViewQuote()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
rs 8.8571
ccs 4
cts 4
cp 1
cc 5
eloc 5
nc 2
nop 1
crap 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A Issue::isQuoteLocked() 0 4 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\Project;
13
14
use Illuminate\Support\Collection;
15
use Tinyissue\Extensions\Auth\LoggedUser;
16
use Tinyissue\Model;
17
use Tinyissue\Model\ModelAbstract;
18
19
/**
20
 * Issue is model class for project issues.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 *
24
 * @property int           $id
25
 * @property int           $created_by
26
 * @property int           $issue_no
27
 * @property string        $issueNumber
28
 * @property int           $project_id
29
 * @property string        $title
30
 * @property string        $body
31
 * @property int           $assigned_to
32
 * @property int           $time_quote
33
 * @property bool          $lock_quote
34
 * @property int           $closed_by
35
 * @property int           $closed_at
36
 * @property int           status
37
 * @property int           $updated_at
38
 * @property int           $updated_by
39
 * @property Model\Project $project
40
 * @property Model\User    $user
41
 * @property Model\User    $assigned
42
 * @property Model\User    $closers
43
 * @property Model\User    $updatedBy
44
 * @property Collection    $attachments
45
 * @property Collection    $activities
46
 * @property Collection    $generalActivities
47
 * @property Collection    $commentActivities
48
 * @property Collection    $tags
49
 * @property Collection    $comments
50
 * @property Collection    $messagesQueue
51
 *
52
 * @method  Model\Tag getStatusTag()
53
 * @method  Model\Tag getTypeTag()
54
 * @method  Model\Tag getResolutionTag()
55
 * @method  Collection getGeneralActivities()
56
 * @method  Collection getCommentActivities()
57
 * @method  int countOpenIssues()
58
 * @method  int countClosedIssues()
59
 * @method  $this open()
60
 * @method  $this closed()
61
 * @method  $this status($status = Issue::STATUS_OPEN)
62
 * @method  $this assignedOrCreated(Model\User $user = null)
63
 * @method  $this assignedTo($user = null)
64
 * @method  $this createdBy(Model\User $user = null)
65
 * @method  $this limitByCreatedForInternalProject(Model\Project $project, Model\User $user = null)
66
 * @method  $this forProject($projectId)
67
 * @method  $this searchContent($keyword)
68
 * @method  $this whereTags(...$tags)
69
 */
70
class Issue extends ModelAbstract
71
{
72
    use IssueRelations,
73
        IssueScopes,
74
        LoggedUser;
75
76
    /**
77
     * Issue status: Open.
78
     *
79
     * @var int
80
     */
81
    const STATUS_OPEN = 1;
82
83
    /**
84
     * Issue status: Closed.
85
     *
86
     * @var int
87
     */
88
    const STATUS_CLOSED = 0;
89
90
    /**
91
     * Timestamp enabled.
92
     *
93
     * @var bool
94
     */
95
    public $timestamps = true;
96
97
    /**
98
     * Name of database table.
99
     *
100
     * @var string
101
     */
102
    protected $table = 'projects_issues';
103
104
    /**
105
     * List of allowed columns to be used in $this->fill().
106
     *
107
     * @var array
108
     */
109
    protected $fillable = ['created_by', 'project_id', 'title', 'body', 'assigned_to', 'time_quote', 'lock_quote'];
110
111
    /**
112
     * Set attributes default value.
113
     *
114
     * @var array
115
     */
116
    protected $attributes = [
117
        'status' => self::STATUS_OPEN,
118
    ];
119
120
    /**
121
     * @param Model\User|null $user
122
     *
123
     * @return \Tinyissue\Repository\Project\Issue\Updater
124
     */
125
    public function updater(Model\User $user = null)
126
    {
127
        return parent::updater($user);
128
    }
129
130
    /**
131
     * Generate a URL for the active project.
132
     *
133
     * @param string $url
134
     *
135
     * @return string
136
     */
137
    public function to($url = '')
138
    {
139
        return \URL::to('project/' . $this->project_id . '/issue/' . $this->id . (($url) ? '/' . $url : ''));
140
    }
141
142
    /**
143
     * Convert time quote from an array into seconds.
144
     *
145
     * @param array $value
146
     */
147
    public function setTimeQuoteAttribute($value)
148
    {
149
        $seconds = $value;
150
        if (is_array($value)) {
151
            $seconds = 0;
152
            $seconds += isset($value['m']) ? ($value['m'] * 60) : 0;
153
            $seconds += isset($value['h']) ? ($value['h'] * 60 * 60) : 0;
154
        }
155
        $this->attributes['time_quote'] = (int)$seconds;
156
    }
157
158
    /**
159
     * Returns the color of tag status.
160
     *
161
     * @return string
162
     */
163
    public function getTypeColorAttribute()
164
    {
165
        $tag = $this->tags->filter(function (Model\Tag $tag) {
166
            return $tag->parent->name === 'type';
167
        })->first();
168
169
        if ($tag) {
170
            return $tag->bgcolor;
171
        }
172
173
        return null;
174
    }
175
176
    /**
177
     * Return issue number
178
     *
179
     * @return string
180
     */
181
    public function getIssueNumberAttribute()
182
    {
183
        return $this->project->key . '-' . $this->issue_no;
184
    }
185
186
    /**
187
     * Whether or not the issue is new.
188
     *
189
     * @return bool
190
     */
191
    public function isNew()
192
    {
193
        if ($this->status === 0) {
194
            return false;
195
        }
196
197
        return $this->tags->count() === 0;
198
    }
199
200
    /**
201
     * Whether or not the issue is open or closed.
202
     *
203
     * @return bool
204
     */
205
    public function isOpen()
206
    {
207
        return (boolean)$this->status;
208
    }
209
210
    /**
211
     * Check if the issue contains a tag with option to set the issue as readonly to current user.
212
     *
213
     * @param Model\User $user
214
     *
215
     * @return bool
216
     */
217
    public function hasReadOnlyTag(Model\User $user)
218
    {
219
        $hasReadOnly = $this->tags->where('readonly', $user->role_id);
220
221
        return !$hasReadOnly->isEmpty();
222
    }
223
224
    /**
225
     * Whether or not the issue quote is locked by manager.
226
     *
227
     * @return bool
228
     */
229
    public function isQuoteLocked()
230
    {
231
        return (boolean)$this->lock_quote;
232
    }
233
234
    /**
235
     * Whether or not a user is the creator of the issue.
236
     *
237
     * @param Model\User $user
238
     *
239
     * @return bool
240
     */
241
    public function isCreatedBy(Model\User $user)
242
    {
243
        return $this->created_by === $user->id;
244
    }
245
}
246