Completed
Push — develop ( 1c3d8e...dc7d05 )
by Mohamed
07:44
created

Issue::isNew()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
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
     * Set attributes default value.
92
     *
93
     * @var array
94
     */
95
    protected $attributes = [
96
        'status' => self::STATUS_OPEN,
97
    ];
98
99
    /**
100
     * Returns the aggregate value of number of comments in an issue.
101
     *
102
     * @return int
103
     */
104 6
    public function getCountCommentsAttribute()
105
    {
106 6
        return $this->getCountAttribute('countComments');
107
    }
108
109
    /**
110
     * Generate a URL for the active project.
111
     *
112
     * @param string $url
113
     *
114
     * @return string
115
     */
116 28
    public function to($url = '')
117
    {
118 28
        return \URL::to('project/' . $this->project_id . '/issue/' . $this->id . (($url) ? '/' . $url : ''));
119
    }
120
121
    /**
122
     * Convert time quote from an array into seconds.
123
     *
124
     * @param array $value
125
     */
126 30
    public function setTimeQuoteAttribute($value)
127
    {
128 30
        $seconds = $value;
129 30
        if (is_array($value)) {
130 29
            $seconds = 0;
131 29
            $seconds += isset($value['m']) ? ($value['m'] * 60) : 0;
132 29
            $seconds += isset($value['h']) ? ($value['h'] * 60 * 60) : 0;
133
        }
134 30
        $this->attributes['time_quote'] = (int) $seconds;
135 30
    }
136
137
    /**
138
     * Returns the color of tag status.
139
     *
140
     * @return string
141
     */
142
    public function getTypeColorAttribute()
143
    {
144
        $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...
145
            return $tag->parent->name === 'type';
146
        })->first();
147
148
        if ($tag) {
149
            return $tag->bgcolor;
150
        }
151
152
        return null;
153
    }
154
155
    /**
156
     * Whether or not the issue is new.
157
     *
158
     * @return bool
159
     */
160
    public function isNew()
161
    {
162
        if ($this->status === 0) {
163
            return false;
164
        }
165
166
        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...
167
    }
168
169
    /**
170
     * Whether or not the issue is open or closed.
171
     *
172
     * @return bool
173
     */
174 24
    public function isOpen()
175
    {
176 24
        return (boolean) $this->status;
177
    }
178
}
179