Completed
Push — develop ( a45f48...f809cc )
by Mohamed
08:29
created

CountTrait::issuesCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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\Traits\Project;
13
14
use Illuminate\Database\Eloquent;
15
use Illuminate\Database\Query;
16
use Tinyissue\Model\Project;
17
18
/**
19
 * CountTrait is trait class containing the methods for counting database records for the Project model.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 *
23
 * @property int   $id
24
 *
25
 * @method   Query\Builder where($column, $operator = null, $value = null, $boolean = 'and')
26
 * @method   Eloquent\Model hasOne($related, $foreignKey = null, $localKey = null)
27
 * @method   RelationTrait issues()
28
 */
29
trait CountTrait
30
{
31
    /**
32
     * Count number of private projects.
33
     *
34
     * @return int
35
     */
36
    public function countPrivateProjects()
37
    {
38
        return $this
39
            ->where('private', '=', Project::PRIVATE_YES)
40
            ->orWhere('private', '=', Project::INTERNAL_YES)->count();
41
    }
42
43
    /**
44
     * Count number of open projects.
45
     *
46
     * @return int
47
     */
48 2
    public function countOpenProjects()
49
    {
50 2
        return $this->where('status', '=', Project::STATUS_OPEN)->count();
51
    }
52
53
    /**
54
     * Count number of archived projects.
55
     *
56
     * @return int
57
     */
58 2
    public function countArchivedProjects()
59
    {
60 2
        return $this->where('status', '=', Project::STATUS_ARCHIVED)->count();
61
    }
62
63
    /**
64
     * Count number of open issue in the project.
65
     *
66
     * @return int
67
     */
68 2
    public function countOpenIssues()
69
    {
70 2
        return Project\Issue::join('projects', 'projects.id', '=', 'projects_issues.project_id')
71 2
            ->where('projects.status', '=', Project::STATUS_OPEN)
72 2
            ->where('projects_issues.status', '=', Project\Issue::STATUS_OPEN)
73 2
            ->count();
74
    }
75
76
    /**
77
     * Count number of closed issue in the project.
78
     *
79
     * @return int
80
     */
81 2
    public function countClosedIssues()
82
    {
83 2
        return Project\Issue::join('projects', 'projects.id', '=', 'projects_issues.project_id')
84 2
            ->where(function (Eloquent\Builder $query) {
85 2
                $query->where('projects.status', '=', Project::STATUS_OPEN);
86 2
                $query->where('projects_issues.status', '=', Project\Issue::STATUS_CLOSED);
87 2
            })
88 2
            ->orWhere('projects_issues.status', '=', Project\Issue::STATUS_CLOSED)
89 2
            ->count();
90
    }
91
92
    /**
93
     * For eager loading: count number of issues.
94
     *
95
     * @return Eloquent\Relations\HasOne
96
     */
97
    public function issuesCount()
98
    {
99
        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...
100
            ->selectRaw('project_id, count(*) as aggregate')
101
            ->groupBy('project_id');
102
    }
103
104
    /**
105
     * For eager loading: include number of closed issues.
106
     *
107
     * @return Eloquent\Relations\HasOne
108
     */
109 36
    public function closedIssuesCount()
110
    {
111
        return $this
112 36
            ->hasOne(
113 36
                'Tinyissue\Model\Project\Issue',
114 36
                'project_id'
115
            )
116 36
            ->selectRaw('project_id, count(*) as aggregate')
117 36
            ->where('status', '=', Project\Issue::STATUS_CLOSED)
118 36
            ->groupBy('project_id');
119
    }
120
121
    /**
122
     * For eager loading: include number of open issues.
123
     *
124
     * @return Eloquent\Relations\HasOne
125
     */
126 37
    public function openIssuesCount()
127
    {
128
        return $this
129 37
            ->hasOne(
130 37
                'Tinyissue\Model\Project\Issue',
131 37
                'project_id'
132
            )
133 37
            ->selectRaw('project_id, count(*) as aggregate')
134 37
            ->where('status', '=', Project\Issue::STATUS_OPEN)
135 37
            ->groupBy('project_id');
136
    }
137
138
    /**
139
     * Return projects with count of open & closed issues.
140
     *
141
     * @param array $projectIds
142
     *
143
     * @return Eloquent\Collection
144
     */
145 1
    public function projectsWithCountIssues(array $projectIds)
146
    {
147
        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...
148 1
            ->with('openIssuesCount', 'closedIssuesCount')
149 1
            ->whereIn('id', $projectIds)
150 1
            ->get();
151
    }
152
153
    /**
154
     * Returns projects with open issue count.
155
     *
156
     * @param int $status
157
     * @param int $private
158
     *
159
     * @return mixed
160
     */
161 5
    public function projectsWithOpenIssuesCount($status = Project::STATUS_OPEN, $private = Project::PRIVATE_YES)
162
    {
163 5
        $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...
164 5
            ->where('status', '=', $status);
165
166 5
        if ($private !== Project::PRIVATE_ALL) {
167 2
            $query->where('private', '=', $private);
168
        }
169
170 5
        return $query;
171
    }
172
}
173