Completed
Push — master ( 195b94...858be2 )
by Mohamed
07:34
created

Issue::setTimeQuoteAttribute()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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