Completed
Push — develop ( 042fea...96dd91 )
by Mohamed
06:47
created

Issue::hasReadOnlyTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\Collection;
15
use Illuminate\Database\Eloquent\Model as BaseModel;
16
use Tinyissue\Model;
17
use Tinyissue\Model\Traits\CountAttributeTrait;
18
use Tinyissue\Model\Traits\Project\Issue\CountTrait;
19
use Tinyissue\Model\Traits\Project\Issue\CrudTrait;
20
use Tinyissue\Model\Traits\Project\Issue\CrudTagTrait;
21
use Tinyissue\Model\Traits\Project\Issue\RelationTrait;
22
use Tinyissue\Model\Traits\Project\Issue\QueryTrait;
23
use Tinyissue\Model\Traits\Project\Issue\QueueTrait;
24
25
/**
26
 * Issue is model class for project issues.
27
 *
28
 * @author Mohamed Alsharaf <[email protected]>
29
 *
30
 * @property int              $id
31
 * @property int              $created_by
32
 * @property int              $project_id
33
 * @property string           $title
34
 * @property string           $body
35
 * @property int              $assigned_to
36
 * @property int              $time_quote
37
 * @property int              $closed_by
38
 * @property int              $closed_at
39
 * @property int              status
40
 * @property int              $updated_at
41
 * @property int              $updated_by
42
 * @property Model\Project    $project
43
 * @property Model\User       $user
44
 * @property Model\User       $updatedBy
45
 */
46
class Issue extends BaseModel
47
{
48
    use CountAttributeTrait,
49
        CountTrait,
50
        CrudTrait,
51
        CrudTagTrait,
52
        RelationTrait,
53
        QueryTrait,
54
        QueueTrait;
55
56
    /**
57
     * Issue status: Open.
58
     *
59
     * @var int
60
     */
61
    const STATUS_OPEN = 1;
62
63
    /**
64
     * Issue status: Closed.
65
     *
66
     * @var int
67
     */
68
    const STATUS_CLOSED = 0;
69
70
    /**
71
     * Timestamp enabled.
72
     *
73
     * @var bool
74
     */
75
    public $timestamps = true;
76
77
    /**
78
     * Name of database table.
79
     *
80
     * @var string
81
     */
82
    protected $table = 'projects_issues';
83
84
    /**
85
     * List of allowed columns to be used in $this->fill().
86
     *
87
     * @var array
88
     */
89
    protected $fillable = ['created_by', 'project_id', 'title', 'body', 'assigned_to', 'time_quote'];
90
91
    /**
92
     * Set attributes default value.
93
     *
94
     * @var array
95
     */
96
    protected $attributes = [
97
        'status' => self::STATUS_OPEN,
98
    ];
99
100
    /**
101
     * Returns the aggregate value of number of comments in an issue.
102
     *
103
     * @return int
104 6
     */
105
    public function getCountCommentsAttribute()
106 6
    {
107
        return $this->getCountAttribute('countComments');
108
    }
109
110
    /**
111
     * Generate a URL for the active project.
112
     *
113
     * @param string $url
114
     *
115
     * @return string
116 29
     */
117
    public function to($url = '')
118 29
    {
119
        return \URL::to('project/' . $this->project_id . '/issue/' . $this->id . (($url) ? '/' . $url : ''));
120
    }
121
122
    /**
123
     * Convert time quote from an array into seconds.
124
     *
125
     * @param array $value
126 31
     */
127
    public function setTimeQuoteAttribute($value)
128 31
    {
129 31
        $seconds = $value;
130 30
        if (is_array($value)) {
131 30
            $seconds = 0;
132 30
            $seconds += isset($value['m']) ? ($value['m'] * 60) : 0;
133
            $seconds += isset($value['h']) ? ($value['h'] * 60 * 60) : 0;
134 31
        }
135 31
        $this->attributes['time_quote'] = (int) $seconds;
136
    }
137
138
    /**
139
     * Returns the color of tag status.
140
     *
141
     * @return string
142
     */
143
    public function getTypeColorAttribute()
144
    {
145
        $tag = $this->tags->filter(function (Model\Tag $tag) {
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Tinyissue\Model\Project\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
146
            return $tag->parent->name === 'type';
147
        })->first();
148
149
        if ($tag) {
150
            return $tag->bgcolor;
151
        }
152
153
        return null;
154
    }
155
156
    /**
157
     * Whether or not the issue is new.
158
     *
159
     * @return bool
160
     */
161
    public function isNew()
162
    {
163
        if ($this->status === 0) {
164
            return false;
165
        }
166
167
        return $this->tags->count() === 0;
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Tinyissue\Model\Project\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
168
    }
169
170
    /**
171
     * Whether or not the issue is open or closed.
172
     *
173
     * @return bool
174 25
     */
175
    public function isOpen()
176 25
    {
177
        return (boolean) $this->status;
178
    }
179
180
    /**
181
     * Check if the issue contains a tag with option to set the issue as readonly to current user.
182
     *
183
     * @param Model\User $user
184
     *
185
     * @return bool
186
     */
187
    public function hasReadOnlyTag(Model\User $user)
188
    {
189
        $hasReadOnly = $this->tags->where('readonly', $user->role_id);
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Tinyissue\Model\Project\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
190
191
        return !$hasReadOnly->isEmpty();
192
    }
193
}
194