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\Traits\Project; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent; |
15
|
|
|
use Illuminate\Database\Query; |
16
|
|
|
use Tinyissue\Model\Project; |
17
|
|
|
use Tinyissue\Model\User; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* CountTrait is trait class containing the methods for counting database records for the Project model. |
21
|
|
|
* |
22
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @property int $id |
25
|
|
|
* |
26
|
|
|
* @method Query\Builder where($column, $operator = null, $value = null, $boolean = 'and') |
27
|
|
|
* @method Eloquent\Model hasOne($related, $foreignKey = null, $localKey = null) |
28
|
|
|
* @method RelationTrait issues() |
29
|
|
|
*/ |
30
|
|
|
trait CountTrait |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Count number of private projects. |
34
|
|
|
* |
35
|
|
|
* @return int |
36
|
|
|
*/ |
37
|
|
|
public function countPrivateProjects() |
38
|
|
|
{ |
39
|
|
|
return $this |
40
|
|
|
->where('private', '=', Project::PRIVATE_YES) |
41
|
|
|
->orWhere('private', '=', Project::INTERNAL_YES)->count(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Count number of open projects. |
46
|
|
|
* |
47
|
|
|
* @return int |
48
|
|
|
*/ |
49
|
2 |
|
public function countOpenProjects() |
50
|
|
|
{ |
51
|
2 |
|
return $this->where('status', '=', Project::STATUS_OPEN)->count(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Count number of archived projects. |
56
|
|
|
* |
57
|
|
|
* @return int |
58
|
|
|
*/ |
59
|
2 |
|
public function countArchivedProjects() |
60
|
|
|
{ |
61
|
2 |
|
return $this->where('status', '=', Project::STATUS_ARCHIVED)->count(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Count number of open issue in the project. |
66
|
|
|
* |
67
|
|
|
* @return int |
68
|
|
|
*/ |
69
|
2 |
|
public function countOpenIssues() |
70
|
|
|
{ |
71
|
2 |
|
return Project\Issue::join('projects', 'projects.id', '=', 'projects_issues.project_id') |
72
|
2 |
|
->where('projects.status', '=', Project::STATUS_OPEN) |
73
|
2 |
|
->where('projects_issues.status', '=', Project\Issue::STATUS_OPEN) |
74
|
2 |
|
->count(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Count number of closed issue in the project. |
79
|
|
|
* |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
2 |
|
public function countClosedIssues() |
83
|
|
|
{ |
84
|
2 |
|
return Project\Issue::join('projects', 'projects.id', '=', 'projects_issues.project_id') |
85
|
2 |
|
->where(function (Eloquent\Builder $query) { |
86
|
2 |
|
$query->where('projects.status', '=', Project::STATUS_OPEN); |
87
|
2 |
|
$query->where('projects_issues.status', '=', Project\Issue::STATUS_CLOSED); |
88
|
2 |
|
}) |
89
|
2 |
|
->orWhere('projects_issues.status', '=', Project\Issue::STATUS_CLOSED) |
90
|
2 |
|
->count(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* For eager loading: count number of issues. |
95
|
|
|
* |
96
|
|
|
* @return Eloquent\Relations\HasOne |
97
|
|
|
*/ |
98
|
|
|
public function issuesCount() |
99
|
|
|
{ |
100
|
|
|
return $this->issues() |
|
|
|
|
101
|
|
|
->selectRaw('project_id, count(*) as aggregate') |
102
|
|
|
->groupBy('project_id'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* For eager loading: include number of closed issues. |
107
|
|
|
* |
108
|
|
|
* @param User $limitByUser |
109
|
|
|
* |
110
|
|
|
* @return Eloquent\Relations\HasOne |
111
|
|
|
*/ |
112
|
22 |
View Code Duplication |
public function closedIssuesCount($limitByUser = null) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$query = $this |
115
|
22 |
|
->hasOne( |
116
|
22 |
|
'Tinyissue\Model\Project\Issue', |
117
|
22 |
|
'project_id' |
118
|
|
|
) |
119
|
22 |
|
->selectRaw('project_id, count(*) as aggregate') |
120
|
22 |
|
->where('status', '=', Project\Issue::STATUS_CLOSED) |
121
|
22 |
|
->groupBy('project_id'); |
122
|
|
|
|
123
|
22 |
|
if ($limitByUser && $limitByUser->isUser() && $this->isPrivateInternal()) { |
|
|
|
|
124
|
|
|
$query->where('created_by', '=', $limitByUser->id); |
125
|
|
|
} |
126
|
|
|
|
127
|
22 |
|
return $query; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* For eager loading: include number of open issues. |
132
|
|
|
* |
133
|
|
|
* @param User $limitByUser |
134
|
|
|
* |
135
|
|
|
* @return Eloquent\Relations\HasOne |
136
|
|
|
*/ |
137
|
23 |
View Code Duplication |
public function openIssuesCount(User $limitByUser = null) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$query = $this |
140
|
23 |
|
->hasOne( |
141
|
23 |
|
'Tinyissue\Model\Project\Issue', |
142
|
23 |
|
'project_id' |
143
|
|
|
) |
144
|
23 |
|
->selectRaw('project_id, count(*) as aggregate') |
145
|
23 |
|
->where('status', '=', Project\Issue::STATUS_OPEN) |
146
|
23 |
|
->groupBy('project_id'); |
147
|
|
|
|
148
|
23 |
|
if ($limitByUser && $limitByUser->isUser() && $this->isPrivateInternal()) { |
|
|
|
|
149
|
|
|
$query->where('created_by', '=', $limitByUser->id); |
150
|
|
|
} |
151
|
|
|
|
152
|
23 |
|
return $query; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Return projects with count of open & closed issues. |
157
|
|
|
* |
158
|
|
|
* @param array $projectIds |
159
|
|
|
* |
160
|
|
|
* @return Eloquent\Collection |
161
|
|
|
*/ |
162
|
1 |
|
public function projectsWithCountIssues(array $projectIds) |
163
|
|
|
{ |
164
|
|
|
return $this |
|
|
|
|
165
|
1 |
|
->with('openIssuesCount', 'closedIssuesCount') |
166
|
1 |
|
->whereIn('id', $projectIds) |
167
|
1 |
|
->get(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns projects with open issue count. |
172
|
|
|
* |
173
|
|
|
* @param int $status |
174
|
|
|
* @param int $private |
175
|
|
|
* |
176
|
|
|
* @return mixed |
177
|
|
|
*/ |
178
|
5 |
|
public function projectsWithOpenIssuesCount($status = Project::STATUS_OPEN, $private = Project::PRIVATE_YES) |
179
|
|
|
{ |
180
|
5 |
|
$query = $this->with('openIssuesCount') |
|
|
|
|
181
|
5 |
|
->where('status', '=', $status); |
182
|
|
|
|
183
|
5 |
|
if ($private !== Project::PRIVATE_ALL) { |
184
|
3 |
|
$query->where('private', '=', $private); |
185
|
|
|
} |
186
|
|
|
|
187
|
5 |
|
return $query; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.