Completed
Push — develop-3.0 ( 4fe777...24fc5d )
by Mohamed
09:15
created

Issue::canUserViewQuote()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 5
nc 2
nop 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 Tinyissue\Model\ModelAbstract;
15
use Illuminate\Support\Collection;
16
use Tinyissue\Contracts\Model\UserInterface;
17
use Tinyissue\Extensions\Auth\LoggedUser;
18
use Tinyissue\Extensions\Model\CountAttributeTrait;
19
use Tinyissue\Model;
20
use Tinyissue\Model\Traits\Project\Issue\QueueTrait;
21
22
/**
23
 * Issue is model class for project issues.
24
 *
25
 * @author Mohamed Alsharaf <[email protected]>
26
 *
27
 * @property int $id
28
 * @property int $created_by
29
 * @property int $project_id
30
 * @property string $title
31
 * @property string $body
32
 * @property int $assigned_to
33
 * @property int $time_quote
34
 * @property bool $lock_quote
35
 * @property int $closed_by
36
 * @property int $closed_at
37
 * @property int status
38
 * @property int $updated_at
39
 * @property int $updated_by
40
 * @property Model\Project $project
41
 * @property Model\User $user
42
 * @property Model\User $assigned
43
 * @property Model\User $closers
44
 * @property Model\User $updatedBy
45
 * @property Collection $attachments
46
 * @property Collection $activities
47
 * @property Collection $generalActivities
48
 * @property Collection $commentActivities
49
 * @property Collection $tags
50
 * @property Collection $comments
51
 * @property Collection $messagesQueue
52
 */
53
class Issue extends ModelAbstract
54
{
55
    use CountAttributeTrait,
56
        IssueRelations,
57
        IssueScopes,
58
//        QueueTrait,
59
        LoggedUser;
60
61
    /**
62
     * Issue status: Open.
63
     *
64
     * @var int
65
     */
66
    const STATUS_OPEN = 1;
67
68
    /**
69
     * Issue status: Closed.
70
     *
71
     * @var int
72
     */
73
    const STATUS_CLOSED = 0;
74
75
    /**
76
     * Timestamp enabled.
77
     *
78
     * @var bool
79
     */
80
    public $timestamps = true;
81
82
    /**
83
     * Name of database table.
84
     *
85
     * @var string
86
     */
87
    protected $table = 'projects_issues';
88
89
    /**
90
     * List of allowed columns to be used in $this->fill().
91
     *
92
     * @var array
93
     */
94
    protected $fillable = ['created_by', 'project_id', 'title', 'body', 'assigned_to', 'time_quote', 'lock_quote'];
95
96
    /**
97
     * Set attributes default value.
98
     *
99
     * @var array
100
     */
101
    protected $attributes = [
102
        'status' => self::STATUS_OPEN,
103
    ];
104
105
    /**
106
     * Returns the aggregate value of number of comments in an issue.
107
     *
108
     * @return int
109
     */
110
    // @todo what if we dont have this do we need it??? we do have relation in Issues::countComments()
111
//    public function getCountCommentsAttribute()
112
//    {
113
//        return $this->getCountAttribute('countComments');
114
//    }
115
116
    /**
117
     * Generate a URL for the active project.
118
     *
119
     * @param string $url
120
     *
121
     * @return string
122
     */
123
    public function to($url = '')
124
    {
125
        return \URL::to('project/' . $this->project_id . '/issue/' . $this->id . (($url) ? '/' . $url : ''));
126
    }
127
128
    /**
129
     * Convert time quote from an array into seconds.
130
     *
131
     * @param array $value
132
     */
133
    public function setTimeQuoteAttribute($value)
134
    {
135
        $seconds = $value;
136
        if (is_array($value)) {
137
            $seconds = 0;
138
            $seconds += isset($value['m']) ? ($value['m'] * 60) : 0;
139
            $seconds += isset($value['h']) ? ($value['h'] * 60 * 60) : 0;
140
        }
141
        $this->attributes['time_quote'] = (int) $seconds;
142
    }
143
144
    /**
145
     * Returns the color of tag status.
146
     *
147
     * @return string
148
     */
149
    public function getTypeColorAttribute()
150
    {
151
        $tag = $this->tags->filter(function (Model\Tag $tag) {
152
            return $tag->parent->name === 'type';
153
        })->first();
154
155
        if ($tag) {
156
            return $tag->bgcolor;
157
        }
158
159
        return null;
160
    }
161
162
    /**
163
     * Whether or not the issue is new.
164
     *
165
     * @return bool
166
     */
167
    public function isNew()
168
    {
169
        if ($this->status === 0) {
170
            return false;
171
        }
172
173
        return $this->tags->count() === 0;
174
    }
175
176
    /**
177
     * Whether or not the issue is open or closed.
178
     *
179
     * @return bool
180
     */
181
    public function isOpen()
182
    {
183
        return (boolean) $this->status;
184
    }
185
186
    /**
187
     * Check if the issue contains a tag with option to set the issue as readonly to current user.
188
     *
189
     * @param UserInterface $user
190
     *
191
     * @return bool
192
     */
193
    public function hasReadOnlyTag(UserInterface $user)
194
    {
195
        $hasReadOnly = $this->tags->where('readonly', $user->role_id);
0 ignored issues
show
Bug introduced by
Accessing role_id on the interface Tinyissue\Contracts\Model\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
196
197
        return !$hasReadOnly->isEmpty();
198
    }
199
200
    /**
201
     * Whether or not the issue quote is locked by manager.
202
     *
203
     * @return bool
204
     */
205
    public function isQuoteLocked()
206
    {
207
        return (boolean) $this->lock_quote;
208
    }
209
210
    /**
211
     * Whether or not a user is the creator of the issue.
212
     *
213
     * @param UserInterface $user
214
     *
215
     * @return bool
216
     */
217
    public function isCreatedBy(UserInterface $user)
218
    {
219
        return $this->created_by === $user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Tinyissue\Contracts\Model\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
220
    }
221
}
222