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\User; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Relations; |
15
|
|
|
use Tinyissue\Model\Project; |
16
|
|
|
use Tinyissue\Model\User; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* CountTrait is trait class containing the methods for counting database records for the User model. |
20
|
|
|
* |
21
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @method bool permission($key) |
24
|
|
|
* @method Relations\HasMany projects($status = Project::STATUS_OPEN) |
25
|
|
|
* @method Relations\HasMany issues() |
26
|
|
|
*/ |
27
|
|
|
trait CountTrait |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @param int $deleted |
31
|
|
|
* |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
2 |
|
public static function countUsers($deleted = User::NOT_DELETED_USERS) |
35
|
|
|
{ |
36
|
2 |
|
return User::where('deleted', '=', $deleted)->count(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Count number of assigned issues in a project. |
41
|
|
|
* |
42
|
|
|
* @param int $projectId |
43
|
|
|
* |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
19 |
View Code Duplication |
public function assignedIssuesCount($projectId = 0) |
|
|
|
|
47
|
|
|
{ |
48
|
19 |
|
$issues = $this->issues(); |
49
|
|
|
|
50
|
19 |
|
if (0 < $projectId) { |
51
|
19 |
|
$issues = $issues->where('project_id', '=', $projectId); |
52
|
|
|
} |
53
|
19 |
|
$issues->where('status', '=', Project\Issue::STATUS_OPEN); |
54
|
|
|
|
55
|
19 |
|
return $issues->count(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Count number of created issues in a project. |
60
|
|
|
* |
61
|
|
|
* @param int $projectId |
62
|
|
|
* |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
2 |
View Code Duplication |
public function createdIssuesCount($projectId = 0) |
|
|
|
|
66
|
|
|
{ |
67
|
2 |
|
$issues = $this->issuesCreatedBy(); |
|
|
|
|
68
|
|
|
|
69
|
2 |
|
if (0 < $projectId) { |
70
|
2 |
|
$issues = $issues->where('project_id', '=', $projectId); |
71
|
|
|
} |
72
|
2 |
|
$issues->where('status', '=', Project\Issue::STATUS_OPEN); |
73
|
|
|
|
74
|
2 |
|
return $issues->count(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns all projects with open issue count. |
79
|
|
|
* |
80
|
|
|
* @param int $status |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
3 |
|
public function projectsWithCountOpenIssues($status = Project::STATUS_OPEN) |
85
|
|
|
{ |
86
|
3 |
|
if ($this->permission('project-all')) { |
87
|
3 |
|
$project = new Project(); |
88
|
|
|
|
89
|
3 |
|
return $project->projectsWithOpenIssuesCount($status, Project::PRIVATE_ALL); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->projects($status)->with('openIssuesCount'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.