Completed
Push — develop ( 64265e...12ceca )
by Mohamed
10:07
created

Issue::isOpen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
ccs 1
cts 1
cp 1
cc 1
eloc 2
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\Project;
13
14
use Illuminate\Database\Eloquent\Model as BaseModel;
15
use Tinyissue\Model;
16
use Tinyissue\Model\Traits\CountAttributeTrait;
17
use Tinyissue\Model\Traits\Project\Issue\CountTrait;
18
use Tinyissue\Model\Traits\Project\Issue\CrudTrait;
19
use Tinyissue\Model\Traits\Project\Issue\CrudTagTrait;
20
use Tinyissue\Model\Traits\Project\Issue\RelationTrait;
21
use Tinyissue\Model\Traits\Project\Issue\QueryTrait;
22
use Tinyissue\Model\Traits\Project\Issue\QueueTrait;
23
24
/**
25
 * Issue is model class for project issues.
26
 *
27
 * @author Mohamed Alsharaf <[email protected]>
28
 *
29
 * @property int              $id
30
 * @property int              $created_by
31
 * @property int              $project_id
32
 * @property string           $title
33
 * @property string           $body
34
 * @property int              $assigned_to
35
 * @property int              $time_quote
36
 * @property int              $closed_by
37
 * @property int              $closed_at
38
 * @property int              status
39
 * @property int              $updated_at
40
 * @property int              $updated_by
41
 * @property Model\Project    $project
42
 * @property Model\User       $user
43
 * @property Model\User       $updatedBy
44
 */
45
class Issue extends BaseModel
46
{
47
    use CountAttributeTrait,
48
        CountTrait,
49
        CrudTrait,
50
        CrudTagTrait,
51
        RelationTrait,
52
        QueryTrait,
53
        QueueTrait;
54
55
    /**
56
     * Issue status: Open.
57
     *
58
     * @var int
59
     */
60
    const STATUS_OPEN = 1;
61
62
    /**
63
     * Issue status: Closed.
64
     *
65
     * @var int
66
     */
67
    const STATUS_CLOSED = 0;
68
69
    /**
70
     * Timestamp enabled.
71
     *
72
     * @var bool
73
     */
74
    public $timestamps = true;
75
76
    /**
77
     * Name of database table.
78
     *
79
     * @var string
80
     */
81
    protected $table = 'projects_issues';
82
83
    /**
84
     * List of allowed columns to be used in $this->fill().
85
     *
86
     * @var array
87
     */
88
    protected $fillable = ['created_by', 'project_id', 'title', 'body', 'assigned_to', 'time_quote'];
89
90
    /**
91
     * Returns the aggregate value of number of comments in an issue.
92
     *
93 6
     * @return int
94
     */
95 6
    public function getCountCommentsAttribute()
96
    {
97
        return $this->getCountAttribute('countComments');
98
    }
99
100
    /**
101
     * Generate a URL for the active project.
102
     *
103
     * @param string $url
104
     *
105 25
     * @return string
106
     */
107 25
    public function to($url = '')
108
    {
109
        return \URL::to('project/' . $this->project_id . '/issue/' . $this->id . (($url) ? '/' . $url : ''));
110
    }
111
112
    /**
113
     * Convert time quote from an array into seconds.
114
     *
115 27
     * @param array $value
116
     */
117 27
    public function setTimeQuoteAttribute($value)
118 27
    {
119 26
        $seconds = $value;
120 26
        if (is_array($value)) {
121 26
            $seconds = 0;
122
            $seconds += isset($value['m']) ? ($value['m'] * 60) : 0;
123 27
            $seconds += isset($value['h']) ? ($value['h'] * 60 * 60) : 0;
124 27
        }
125
        $this->attributes['time_quote'] = (int) $seconds;
126
    }
127
128
    /**
129
     * Returns the color of tag status.
130
     *
131
     * @return string
132
     */
133
    public function getTypeColorAttribute()
134
    {
135
        $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...
136
            return $tag->parent->name === 'type';
137
        })->first();
138
139
        if ($tag) {
140
            return $tag->bgcolor;
141
        }
142
143
        return null;
144
    }
145
146
    /**
147
     * Whether or not the issue is new.
148
     *
149
     * @return bool
150
     */
151
    public function isNew()
152
    {
153
        if ($this->status === 0) {
154
            return false;
155
        }
156
157
        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...
158
    }
159
160
    /**
161
     * Whether or not the issue is open or closed.
162
     *
163 16
     * @return bool
164
     */
165 16
    public function isOpen()
166
    {
167
        return (boolean) $this->status;
168
    }
169
}
170