Completed
Branch develop (d0c38b)
by Mohamed
03:48
created

CountTrait::countOpenIssues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
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()
0 ignored issues
show
Bug introduced by
It seems like selectRaw() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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 37
    public function closedIssuesCount(User $limitByUser = null)
113
    {
114
        return $this->issuesCountByStatus(Project\Issue::STATUS_CLOSED, $limitByUser);
115 37
    }
116 37
117 37
    /**
118
     * For eager loading: include number of open issues.
119 37
     *
120 37
     * @param User $limitByUser
121 37
     *
122
     * @return Eloquent\Relations\HasOne
123 37
     */
124
    public function openIssuesCount(User $limitByUser = null)
125
    {
126
        return $this->issuesCountByStatus(Project\Issue::STATUS_OPEN, $limitByUser);
127 37
    }
128
129
    /**
130
     * For eager loading: include number of issues by open/closed status.
131
     *
132
     * @param int       $status
133
     * @param User|null $limitByUser
134
     *
135
     * @return Eloquent\Relations\HasOne
136
     */
137 38
    protected function issuesCountByStatus($status, User $limitByUser = null)
138
    {
139
        $query = $this
140 38
            ->hasOne(
141 38
                'Tinyissue\Model\Project\Issue',
142 38
                'project_id'
143
            )
144 38
            ->selectRaw('project_id, count(*) as aggregate')
145 38
            ->where('status', '=', $status)
146 38
            ->groupBy('project_id');
147
148 38
        if ($limitByUser && $limitByUser->isUser() && $this->isPrivateInternal()) {
0 ignored issues
show
Bug introduced by
It seems like isPrivateInternal() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
149
            $query->where('created_by', '=', $limitByUser->id);
150
        }
151
152 38
        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
0 ignored issues
show
Bug introduced by
It seems like with() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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 6
    public function projectsWithOpenIssuesCount($status = Project::STATUS_OPEN, $private = Project::PRIVATE_YES)
179
    {
180 6
        $query = $this->with('openIssuesCount')
0 ignored issues
show
Bug introduced by
It seems like with() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
181 6
            ->where('status', '=', $status);
182
183 6
        if ($private !== Project::PRIVATE_ALL) {
184 3
            $query->where('private', '=', $private);
185
        }
186
187 6
        return $query;
188
    }
189
}
190