Completed
Push — master ( 91f8d9...64265e )
by Mohamed
09:45 queued 06:57
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 1
Bugs 0 Features 0
Metric Value
c 1
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->where('private', '=', Project::PRIVATE_YES)->count();
39
    }
40
41
    /**
42
     * Count number of open projects.
43
     *
44
     * @return int
45
     */
46 2
    public function countOpenProjects()
47
    {
48 2
        return $this->where('status', '=', Project::STATUS_OPEN)->count();
49
    }
50
51
    /**
52
     * Count number of archived projects.
53
     *
54
     * @return int
55
     */
56 2
    public function countArchivedProjects()
57
    {
58 2
        return $this->where('status', '=', Project::STATUS_ARCHIVED)->count();
59
    }
60
61
    /**
62
     * Count number of open issue in the project.
63
     *
64
     * @return int
65
     */
66 2
    public function countOpenIssues()
67
    {
68 2
        return Project\Issue::join('projects', 'projects.id', '=', 'projects_issues.project_id')
69 2
            ->where('projects.status', '=', Project::STATUS_OPEN)
70 2
            ->where('projects_issues.status', '=', Project\Issue::STATUS_OPEN)
71 2
            ->count();
72
    }
73
74
    /**
75
     * Count number of closed issue in the project.
76
     *
77
     * @return int
78
     */
79 2
    public function countClosedIssues()
80
    {
81 2
        return Project\Issue::join('projects', 'projects.id', '=', 'projects_issues.project_id')
82 2
            ->where(function (Eloquent\Builder $query) {
83 2
                $query->where('projects.status', '=', Project::STATUS_OPEN);
84 2
                $query->where('projects_issues.status', '=', Project\Issue::STATUS_CLOSED);
85 2
            })
86 2
            ->orWhere('projects_issues.status', '=', Project\Issue::STATUS_CLOSED)
87 2
            ->count();
88
    }
89
90
    /**
91
     * For eager loading: count number of issues.
92
     *
93
     * @return Eloquent\Relations\HasOne
94
     */
95
    public function issuesCount()
96
    {
97
        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...
98
            ->selectRaw('project_id, count(*) as aggregate')
99
            ->groupBy('project_id');
100
    }
101
102
    /**
103
     * For eager loading: include number of closed issues.
104
     *
105
     * @return Eloquent\Relations\HasOne
106
     */
107 36
    public function closedIssuesCount()
108
    {
109
        return $this
110 36
            ->hasOne(
111 36
                'Tinyissue\Model\Project\Issue',
112 36
                'project_id'
113
            )
114 36
            ->selectRaw('project_id, count(*) as aggregate')
115 36
            ->where('status', '=', Project\Issue::STATUS_CLOSED)
116 36
            ->groupBy('project_id');
117
    }
118
119
    /**
120
     * For eager loading: include number of open issues.
121
     *
122
     * @return Eloquent\Relations\HasOne
123
     */
124 37
    public function openIssuesCount()
125
    {
126
        return $this
127 37
            ->hasOne(
128 37
                'Tinyissue\Model\Project\Issue',
129 37
                'project_id'
130
            )
131 37
            ->selectRaw('project_id, count(*) as aggregate')
132 37
            ->where('status', '=', Project\Issue::STATUS_OPEN)
133 37
            ->groupBy('project_id');
134
    }
135
136
    /**
137
     * Return projects with count of open & closed issues.
138
     *
139
     * @param array $projectIds
140
     *
141
     * @return Eloquent\Collection
142
     */
143 1
    public function projectsWithCountIssues(array $projectIds)
144
    {
145
        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...
146 1
            ->with('openIssuesCount', 'closedIssuesCount')
147 1
            ->whereIn('id', $projectIds)
148 1
            ->get();
149
    }
150
151
    /**
152
     * Returns projects with open issue count.
153
     *
154
     * @param int $status
155
     * @param int $private
156
     *
157
     * @return mixed
158
     */
159 5
    public function projectsWithOpenIssuesCount($status = Project::STATUS_OPEN, $private = Project::PRIVATE_YES)
160
    {
161 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...
162 5
            ->where('status', '=', $status);
163
164 5
        if ($private !== Project::PRIVATE_ALL) {
165 2
            $query->where('private', '=', $private);
166
        }
167
168 5
        return $query;
169
    }
170
}
171