1 | <?php |
||
25 | trait CountTrait |
||
26 | { |
||
27 | /** |
||
28 | * Count number of private projects. |
||
29 | * |
||
30 | * @return int |
||
31 | */ |
||
32 | public function countPrivateProjects() |
||
33 | { |
||
34 | return $this |
||
|
|||
35 | ->where('private', '=', Project::PRIVATE_YES) |
||
36 | ->orWhere('private', '=', Project::INTERNAL_YES)->count(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Count number of open projects. |
||
41 | * |
||
42 | * @return int |
||
43 | */ |
||
44 | 2 | public function countOpenProjects() |
|
45 | { |
||
46 | 2 | return $this->where('status', '=', Project::STATUS_OPEN)->count(); |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Count number of archived projects. |
||
51 | * |
||
52 | * @return int |
||
53 | */ |
||
54 | 2 | public function countArchivedProjects() |
|
55 | { |
||
56 | 2 | return $this->where('status', '=', Project::STATUS_ARCHIVED)->count(); |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Count number of open issue in the project. |
||
61 | * |
||
62 | * @return int |
||
63 | */ |
||
64 | 2 | public function countOpenIssues() |
|
65 | { |
||
66 | 2 | return Project\Issue::join('projects', 'projects.id', '=', 'projects_issues.project_id') |
|
67 | 2 | ->where('projects.status', '=', Project::STATUS_OPEN) |
|
68 | 2 | ->where('projects_issues.status', '=', Project\Issue::STATUS_OPEN) |
|
69 | 2 | ->count(); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Count number of closed issue in the project. |
||
74 | * |
||
75 | * @return int |
||
76 | */ |
||
77 | 2 | public function countClosedIssues() |
|
78 | { |
||
79 | 2 | return Project\Issue::join('projects', 'projects.id', '=', 'projects_issues.project_id') |
|
80 | 2 | ->where(function (Eloquent\Builder $query) { |
|
81 | 2 | $query->where('projects.status', '=', Project::STATUS_OPEN); |
|
82 | 2 | $query->where('projects_issues.status', '=', Project\Issue::STATUS_CLOSED); |
|
83 | 2 | }) |
|
84 | 2 | ->orWhere('projects_issues.status', '=', Project\Issue::STATUS_CLOSED) |
|
85 | 2 | ->count(); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * For eager loading: count number of issues. |
||
90 | * |
||
91 | * @return Eloquent\Relations\HasOne |
||
92 | */ |
||
93 | public function issuesCount() |
||
94 | { |
||
95 | return $this->issues() |
||
96 | ->selectRaw('project_id, count(*) as aggregate') |
||
97 | ->groupBy('project_id'); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * For eager loading: include number of closed issues. |
||
102 | * |
||
103 | * @param User $limitByUser |
||
104 | * |
||
105 | * @return Eloquent\Relations\HasOne |
||
106 | */ |
||
107 | 36 | public function closedIssuesCount(User $limitByUser = null) |
|
108 | { |
||
109 | 36 | return $this->issuesCountByStatus(Project\Issue::STATUS_CLOSED, $limitByUser); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * For eager loading: include number of open issues. |
||
114 | * |
||
115 | * @param User $limitByUser |
||
116 | * |
||
117 | * @return Eloquent\Relations\HasOne |
||
118 | */ |
||
119 | 38 | public function openIssuesCount(User $limitByUser = null) |
|
120 | { |
||
121 | 38 | return $this->issuesCountByStatus(Project\Issue::STATUS_OPEN, $limitByUser); |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * For eager loading: include number of issues by open/closed status. |
||
126 | * |
||
127 | * @param int $status |
||
128 | * @param User|null $limitByUser |
||
129 | * |
||
130 | * @return Eloquent\Relations\HasOne |
||
131 | */ |
||
132 | 38 | protected function issuesCountByStatus($status, User $limitByUser = null) |
|
133 | { |
||
134 | $query = $this |
||
135 | 38 | ->hasOne( |
|
136 | 38 | 'Tinyissue\Model\Project\Issue', |
|
137 | 38 | 'project_id' |
|
138 | ) |
||
139 | 38 | ->selectRaw('project_id, count(*) as aggregate') |
|
140 | 38 | ->where('status', '=', $status) |
|
141 | 38 | ->groupBy('project_id'); |
|
142 | |||
143 | 38 | if ($limitByUser && $limitByUser->isUser() && $this->isPrivateInternal()) { |
|
144 | $query->where('created_by', '=', $limitByUser->id); |
||
145 | } |
||
146 | |||
147 | 38 | return $query; |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Return projects with count of open & closed issues. |
||
152 | * |
||
153 | * @param array $projectIds |
||
154 | * |
||
155 | * @return Eloquent\Collection |
||
156 | */ |
||
157 | 1 | public function projectsWithCountIssues(array $projectIds) |
|
158 | { |
||
159 | return $this |
||
160 | 1 | ->with('openIssuesCount', 'closedIssuesCount') |
|
161 | 1 | ->whereIn('id', $projectIds) |
|
162 | 1 | ->get(); |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Returns projects with open issue count. |
||
167 | * |
||
168 | * @param int $status |
||
169 | * @param int $private |
||
170 | * |
||
171 | * @return mixed |
||
172 | */ |
||
173 | 6 | public function projectsWithOpenIssuesCount($status = Project::STATUS_OPEN, $private = Project::PRIVATE_YES) |
|
174 | { |
||
175 | 6 | $query = $this->with('openIssuesCount') |
|
176 | 6 | ->where('status', '=', $status); |
|
177 | |||
178 | 6 | if ($private !== Project::PRIVATE_ALL) { |
|
179 | 3 | $query->where('private', '=', $private); |
|
180 | } |
||
181 | |||
182 | 6 | return $query; |
|
183 | } |
||
184 | |||
185 | abstract public function hasOne($related, $foreignKey = null, $localKey = null); |
||
187 | } |
||
188 |
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.