Completed
Push — develop ( fea319...7a0090 )
by Mohamed
07:15
created

Project::to()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
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;
13
14
use Illuminate\Database\Eloquent\Model;
15
use URL;
16
17
/**
18
 * Project is model class for projects.
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 *
22
 * @property int              $id
23
 * @property string           $name
24
 * @property int              $status
25
 * @property int              $default_assignee
26
 * @property Project\Issue[]  $issues
27
 * @property int              $openIssuesCount
28
 * @property int              $closedIssuesCount
29
 */
30
class Project extends Model
31
{
32
    use Traits\CountAttributeTrait,
33
        Traits\Project\CountTrait,
34
        Traits\Project\FilterTrait,
35
        Traits\Project\SortTrait,
36
        Traits\Project\RelationTrait,
37
        Traits\Project\CrudTrait,
38
        Traits\Project\QueryTrait;
39
40
    /**
41
     * Project not public to view and create issue.
42
     *
43
     * @var int
44
     */
45
    const PRIVATE_YES = 1;
46
47
    /**
48
     * Project public to view and create issue.
49
     *
50
     * @var int
51
     */
52
    const PRIVATE_NO = 0;
53
54
    /**
55
     * All projects.
56
     *
57
     * @var int
58
     */
59
    const PRIVATE_ALL = -1;
60
61
    /**
62
     * Project status Open.
63
     *
64
     * @var int
65
     */
66
    const STATUS_OPEN = 1;
67
68
    /**
69
     * Project status Archived.
70
     *
71
     * @var int
72
     */
73
    const STATUS_ARCHIVED = 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';
88
89
    /**
90
     * List of allowed columns to be used in $this->fill().
91
     *
92
     * @var array
93
     */
94
    protected $fillable = ['name', 'default_assignee', 'status', 'private'];
95
96
    /**
97
     * Generate a URL for the active project.
98
     *
99
     * @param string $url
100
     *
101
     * @return string
102
     */
103 37
    public function to($url = '')
104
    {
105 37
        return URL::to('project/' . $this->id . (($url) ? '/' . $url : ''));
106
    }
107
108
    /**
109
     * Returns the aggregate value of number of open issues in the project.
110
     *
111
     * @return int
112
     */
113 4
    public function getOpenIssuesCountAttribute()
114
    {
115 4
        return $this->getCountAttribute('openIssuesCount');
116
    }
117
118
    /**
119
     * Returns the aggregate value of number of closed issues in the project.
120
     *
121
     * @return int
122
     */
123
    public function getClosedIssuesCountAttribute()
124
    {
125
        return $this->getCountAttribute('closedIssuesCount');
126
    }
127
128
    /**
129
     * Set default assignee attribute.
130
     *
131
     * @param int $value
132
     *
133
     * @return $this
134
     */
135 46
    public function setDefaultAssigneeAttribute($value)
136
    {
137 46
        if (!empty($value)) {
138 28
            $this->attributes['default_assignee'] = (int) $value;
139
        }
140
141 46
        return $this;
142
    }
143
144
    /**
145
     * Returns the aggregate value of number of issues in the project.
146
     *
147
     * @return int
148
     */
149
    public function getIssuesCountAttribute()
150
    {
151
        return $this->getCountAttribute('issuesCount');
152
    }
153
154
    /**
155
     * Get total issues total quote time.
156
     *
157
     * @return int
158
     */
159 1
    public function getTotalQuote()
160
    {
161 1
        $total = 0;
162 1
        foreach ($this->issues as $issue) {
163 1
            $total += $issue->time_quote;
164
        }
165
166 1
        return $total;
167
    }
168
169
    /**
170
     * Calculate the progress (open & closed issues).
171
     *
172
     * @return float|int
173
     */
174
    public function getProgress()
175
    {
176
        $total       = $this->openIssuesCount + $this->closedIssuesCount;
177
        $progress    = 100;
178
        if ($total > 0) {
179
            $progress = (float) ($this->closedIssuesCount / $total) * 100;
180
        }
181
        $progressInt = (int) $progress;
182
        if ($progressInt > 0) {
183
            $progress = number_format($progress, 2);
184
            $fraction = $progress - $progressInt;
185
            if ($fraction === 0.0) {
186
                $progress = $progressInt;
187
            }
188
        }
189
190
        return $progress;
191
    }
192
193
    /**
194
     * Whether or not a user is member of the project.
195
     *
196
     * @param int $userId
197
     *
198
     * @return bool
199
     */
200 8
    public function isMember($userId)
201
    {
202 8
        return $this->user($userId)->count() > 0;
203
    }
204
205
    /**
206
     * Whether or not the project is private or public.
207
     *
208
     * @return bool
209
     */
210 1
    public function isPrivate()
211
    {
212 1
        return $this->private === true;
0 ignored issues
show
Documentation introduced by
The property private does not exist on object<Tinyissue\Model\Project>. 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...
213
    }
214
}
215