Completed
Branch master (7a36f1)
by Mohamed
04:08
created

CountTrait::issuesCountByStatus()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 17
ccs 9
cts 10
cp 0.9
rs 9.2
cc 4
eloc 11
nc 2
nop 2
crap 4.016
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 Tinyissue\Model\Project;
16
use Tinyissue\Model\User;
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 static $this
24
 */
25
trait CountTrait
26
{
27
    /**
28
     * Count number of private projects.
29
     *
30
     * @return int
31
     */
32
    public function countPrivateProjects()
33
    {
34
        return $this
0 ignored issues
show
Bug introduced by
It seems like where() 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...
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();
0 ignored issues
show
Bug introduced by
It seems like where() 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...
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();
0 ignored issues
show
Bug introduced by
It seems like where() 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...
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()
0 ignored issues
show
Bug introduced by
The method issues() does not exist on Tinyissue\Model\Traits\Project\CountTrait. Did you maybe mean countOpenIssues()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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
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...
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')
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...
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);
186
    abstract public function isPrivateInternal();
187
}
188