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

QueryTrait::listIssues()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3.0105

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 30
ccs 17
cts 19
cp 0.8947
rs 8.8571
cc 3
eloc 19
nc 3
nop 2
crap 3.0105
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\Eloquent\Relations;
16
use Illuminate\Database\Eloquent\Relations\Relation;
17
use Tinyissue\Model\Project;
18
use Tinyissue\Model\User;
19
20
/**
21
 * QueryTrait is trait class containing the database queries methods for the Project model.
22
 *
23
 * @author Mohamed Alsharaf <[email protected]>
24
 *
25
 * @property static $this
26
 */
27
trait QueryTrait
28
{
29
    /**
30
     * Returns collection of active projects.
31
     *
32
     * @return Eloquent\Collection
33
     */
34
    public static function activeProjects()
35
    {
36
        return static::where('status', '=', Project::STATUS_OPEN)
37
            ->orderBy('name', 'ASC')
38
            ->get();
39
    }
40
41
    /**
42
     * Returns collection of public projects.
43
     *
44
     * @return Eloquent\Collection
45
     */
46
    public function publicProjects()
47
    {
48
        return $this->where('private', '=', Project::PRIVATE_NO)
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...
49
            ->orderBy('name', 'ASC')
50
            ->get();
51
    }
52
53
    /**
54
     * Returns all users that are not assigned in the current project.
55
     *
56
     * @return array
57
     */
58 1
    public function usersNotIn()
59
    {
60 1
        if ($this->id > 0) {
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
            $userIds = $this->users()->lists('user_id')->all();
62
            $users   = User::where('deleted', '=', User::NOT_DELETED_USERS)->whereNotIn('id', $userIds)->get();
63
        } else {
64 1
            $users = User::where('deleted', '=', User::NOT_DELETED_USERS)->get();
65
        }
66
67 1
        return $users->lists('fullname', 'id')->all();
68
    }
69
70
    /**
71
     * Fetch and filter issues in the project.
72
     *
73
     * @param int   $status
74
     * @param array $filter
75
     *
76
     * @return \Illuminate\Database\Eloquent\Collection
77
     */
78 2
    public function listIssues($status = Project\Issue::STATUS_OPEN, array $filter = [])
79
    {
80 2
        $sortOrder = array_get($filter, 'sort.sortorder', 'desc');
81 2
        $sortBy    = array_get($filter, 'sort.sortby', null);
82
83 2
        $query = $this->issues()
0 ignored issues
show
Bug introduced by
The method issues() does not exist on Tinyissue\Model\Traits\Project\QueryTrait. Did you maybe mean listIssues()?

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...
84 2
            ->with('countComments', 'user', 'updatedBy', 'tags', 'tags.parent')
85 2
            ->with([
86
                'tags' => function (Relation $query) use ($sortOrder) {
87 2
                    $query->orderBy('name', $sortOrder);
88 2
                },
89
            ])
90 2
            ->where('status', '=', $status);
91
92
        // Filter issues
93 2
        $this->filterAssignTo($query, array_get($filter, 'assignto'));
94 2
        $this->filterTitleOrBody($query, array_get($filter, 'keyword'));
95 2
        $this->filterTags($query, array_get($filter, 'tag_status'));
96 2
        $this->filterTags($query, array_get($filter, 'tag_type'));
97 2
        $this->filterCreatedBy($query, array_get($filter, 'created_by'), $this->isPrivateInternal());
98
99
        // Sort
100 2
        if ($sortBy == 'updated') {
101
            $this->sortByUpdated($query, $sortOrder);
102 2
        } elseif (($tagGroup = substr($sortBy, strlen('tag:'))) > 0) {
103
            return $this->sortByTag($query, $tagGroup, $sortOrder);
104
        }
105
106 2
        return $query->get();
107
    }
108
109
    /**
110
     * Fetch issues assigned to a user.
111
     *
112
     * @param User $user
113
     *
114
     * @return \Illuminate\Database\Eloquent\Collection
115
     */
116 1
    public function listAssignedOrCreatedIssues(User $user)
117
    {
118 1
        $assignedOrCreate = $user->isUser() ? 'created_by' : 'assigned_to';
119
120 1
        return $this->issues()
0 ignored issues
show
Bug introduced by
The method issues() does not exist on Tinyissue\Model\Traits\Project\QueryTrait. Did you maybe mean listIssues()?

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...
121 1
            ->with('countComments', 'user', 'updatedBy')
122 1
            ->where('status', '=', Project\Issue::STATUS_OPEN)
123 1
            ->where($assignedOrCreate, '=', $user->id)
124 1
            ->orderBy('updated_at', 'DESC')
125 1
            ->get();
126
    }
127
128
    /**
129
     * Returns projects with issues details eager loaded.
130
     *
131
     * @param int $status
132
     * @param int $private
133
     *
134
     * @return Relations\HasMany
135
     */
136 4
    public function projectsWidthIssues($status = Project::STATUS_OPEN, $private = Project::PRIVATE_NO)
137
    {
138
        $query = $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...
139 4
            ->where('status', '=', $status)
140 4
            ->orderBy('name');
141
142 4
        if ($private !== Project::PRIVATE_ALL) {
143 4
            $query->where('private', '=', $private);
144
        }
145
146 4
        $query->with([
147
            'issues' => function (Relations\Relation $query) use ($status) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
148 3
                $query->with('updatedBy');
149 3
                if ($status === Project::STATUS_OPEN) {
150 3
                    $query->where('status', '=', Project\Issue::STATUS_OPEN);
151
                }
152 4
            },
153
            'issues.user' => function () {
154 4
            },
155
            'issues.countComments' => function () {
156 4
            },
157
        ]);
158
159 4
        return $query;
160
    }
161
162
    /**
163
     * Returns collection of tags for Kanban view.
164
     *
165
     * @param User $user
166
     *
167
     * @return mixed
168
     */
169 1
    public function getKanbanTagsForUser(User $user)
170
    {
171 1
        $tags = $this->kanbanTags()
0 ignored issues
show
Bug introduced by
The method kanbanTags() does not exist on Tinyissue\Model\Traits\Project\QueryTrait. Did you maybe mean getKanbanTagsForUser()?

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...
172
            ->where(function (Eloquent\Builder $query) use ($user) {
173 1
                $query->where('role_limit', '<=', $user->role_id);
174 1
                $query->orWhere('role_limit', '=', null);
175 1
            })
176 1
            ->get();
177
178 1
        return $tags;
179
    }
180
181
    /**
182
     * Returns collection of issues grouped by tags.
183
     *
184
     * @param $tagIds
185
     *
186
     * @return mixed
187
     */
188
    public function issuesGroupByTags($tagIds)
189
    {
190
        $issues = $this->issues()
0 ignored issues
show
Bug introduced by
The method issues() does not exist on Tinyissue\Model\Traits\Project\QueryTrait. Did you maybe mean listIssues()?

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...
191
        ->with('user', 'tags')
192
            ->where('status', '=', Project\Issue::STATUS_OPEN)
193
            ->whereIn('projects_issues_tags.tag_id', $tagIds)
194
            ->join('projects_issues_tags', 'issue_id', '=', 'id')
195
            ->orderBy('id')
196
            ->get()
197
            ->groupBy(function (Project\Issue $issue) {
198
                return $issue->getStatusTag()->name;
199
            });
200
201
        return $issues;
202
    }
203
204
    /**
205
     * Returns users assigned to the project that can fix issues (with edit permission).
206
     *
207
     * @return Relations\BelongsToMany
208
     */
209 18
    public function usersCanFixIssue()
210
    {
211 18
        return $this->users()->where('users.role_id', '>', 1)->where('users.deleted', '=', User::NOT_DELETED_USERS);
212
    }
213
214
    abstract public function users();
215
    abstract public function filterAssignTo(Relations\HasMany $query, $userId);
216
    abstract public function filterTitleOrBody(Relations\HasMany $query, $keyword);
217
    abstract public function filterTags(Relations\HasMany $query, $tags);
218
    abstract public function isPrivateInternal();
219
    abstract public function filterCreatedBy(Relations\HasMany $query, $userId, $enabled = false);
220
    abstract public function sortByUpdated(Relations\HasMany $query, $order = 'asc');
221
    abstract public function sortByTag(Relations\HasMany $query, $tagGroup, $order = 'asc');
222
}
223