Completed
Push — develop ( 53566b...c11cc2 )
by Mohamed
07:45
created

QueryTrait::listIssues()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.0123

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 29
ccs 16
cts 18
cp 0.8889
rs 8.8571
cc 3
eloc 18
nc 3
nop 2
crap 3.0123
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 Illuminate\Database\Query;
18
use Tinyissue\Model\Project;
19
use Tinyissue\Model\Tag;
20
use Tinyissue\Model\User;
21
22
/**
23
 * QueryTrait is trait class containing the database queries methods for the Project model.
24
 *
25
 * @author Mohamed Alsharaf <[email protected]>
26
 *
27
 * @property int $id
28
 *
29
 * @method   Eloquent\Model      where($column, $operator = null, $value = null, $boolean = 'and')
30
 * @method   Query\Builder       join($table, $one, $operator = null, $two = null, $type = 'inner', $where = false)
31
 * @method   Relations\HasMany   users()
32
 * @method   Relations\HasMany   issues()
33
 * @method   void                filterAssignTo(Query\Builder $query, $userId)
34
 * @method   void                filterTitleOrBody(Query\Builder $query, $keyword)
35
 * @method   void                filterTags(Eloquent\Builder $query, array $tags)
36
 * @method   void                sortByUpdated(Query\Builder $query, $order = 'asc')
37
 * @method   Eloquent\Collection sortByTag(Query\Builder $query, $tagGroup, $order = 'asc')
38
 */
39
trait QueryTrait
40
{
41
    /**
42
     * Returns collection of active projects.
43
     *
44
     * @return Eloquent\Collection
45
     */
46
    public static function activeProjects()
47
    {
48
        return static::where('status', '=', Project::STATUS_OPEN)
49
            ->orderBy('name', 'ASC')
50
            ->get();
51
    }
52
53
    /**
54
     * Returns collection of public projects.
55
     *
56
     * @return Eloquent\Collection
57
     */
58
    public function publicProjects()
59
    {
60
        return $this->where('private', '=', Project::PRIVATE_NO)
61
            ->orderBy('name', 'ASC')
62
            ->get();
63
    }
64
65
    /**
66
     * Returns all users that are not assigned in the current project.
67
     *
68
     * @return array
69
     */
70 1
    public function usersNotIn()
71
    {
72 1
        if ($this->id > 0) {
73
            $userIds = $this->users()->lists('user_id')->all();
74
            $users   = User::where('deleted', '=', User::NOT_DELETED_USERS)->whereNotIn('id', $userIds)->get();
75
        } else {
76 1
            $users = User::where('deleted', '=', User::NOT_DELETED_USERS)->get();
77
        }
78
79 1
        return $users->lists('fullname', 'id')->all();
80
    }
81
82
    /**
83
     * Fetch and filter issues in the project.
84
     *
85
     * @param int   $status
86
     * @param array $filter
87
     *
88
     * @return \Illuminate\Database\Eloquent\Collection
89
     */
90 2
    public function listIssues($status = Project\Issue::STATUS_OPEN, array $filter = [])
91
    {
92 2
        $sortOrder = array_get($filter, 'sort.sortorder', 'desc');
93 2
        $sortBy    = array_get($filter, 'sort.sortby', null);
94
95 2
        $query = $this->issues()
96 2
            ->with('countComments', 'user', 'updatedBy', 'tags', 'tags.parent')
97 2
            ->with([
98
                'tags' => function (Relation $query) use ($sortOrder) {
99 2
                    $query->orderBy('name', $sortOrder);
100 2
                },
101
            ])
102 2
            ->where('status', '=', $status);
103
104
        // Filter issues
105 2
        $this->filterAssignTo($query, array_get($filter, 'assignto'));
106 2
        $this->filterTitleOrBody($query, array_get($filter, 'keyword'));
107 2
        $this->filterTags($query, array_get($filter, 'tag_status'));
108 2
        $this->filterTags($query, array_get($filter, 'tag_type'));
109
110
        // Sort
111 2
        if ($sortBy == 'updated') {
112
            $this->sortByUpdated($query, $sortOrder);
113 2
        } elseif (($tagGroup = substr($sortBy, strlen('tag:'))) > 0) {
114
            return $this->sortByTag($query, $tagGroup, $sortOrder);
115
        }
116
117 2
        return $query->get();
118
    }
119
120
    /**
121
     * Fetch issues assigned to a user.
122
     *
123
     * @param int $userId
124
     *
125
     * @return \Illuminate\Database\Eloquent\Collection
126
     */
127 1
    public function listAssignedIssues($userId)
128
    {
129 1
        return $this->issues()
130 1
            ->with('countComments', 'user', 'updatedBy')
131 1
            ->where('status', '=', Project\Issue::STATUS_OPEN)
132 1
            ->where('assigned_to', '=', $userId)
133 1
            ->orderBy('updated_at', 'DESC')
134 1
            ->get();
135
    }
136
137
    /**
138
     * Returns projects with issues details eager loaded.
139
     *
140
     * @param int $status
141
     * @param int $private
142
     *
143
     * @return Relations\HasMany
144
     */
145 4
    public function projectsWidthIssues($status = Project::STATUS_OPEN, $private = Project::PRIVATE_NO)
146
    {
147
        $query = $this
148 4
            ->where('status', '=', $status)
149 4
            ->orderBy('name');
150
151 4
        if ($private !== Project::PRIVATE_ALL) {
152 4
            $query->where('private', '=', $private);
153
        }
154
155 4
        $query->with([
156 View Code Duplication
            '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...
157 3
                $query->with('updatedBy');
158 3
                if ($status === Project::STATUS_OPEN) {
159 3
                    $query->where('status', '=', Project\Issue::STATUS_OPEN);
160
                }
161 4
            },
162
            'issues.user' => function () {
163 4
            },
164
            'issues.countComments' => function () {
165 4
            },
166
        ]);
167
168 4
        return $query;
169
    }
170
171
    /**
172
     * Returns collection of tags for Kanban view.
173
     *
174
     * @param User $user
175
     *
176
     * @return mixed
177
     */
178 5
    public function getKanbanTagsForUser(User $user)
179
    {
180 5
        $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...
181
            ->where(function ($query) use ($user) {
182 5
                $query->where('role_limit', '<=', $user->role_id);
183 5
                $query->orWhere('role_limit', '=', null);
184 5
            })
185 5
            ->get();
186
187 5
        return $tags;
188
    }
189
190
    /**
191
     * Returns collection of issues grouped by tags.
192
     *
193
     * @param $tagIds
194
     *
195
     * @return mixed
196
     */
197
    public function issuesGroupByTags($tagIds)
198
    {
199
        $issues = $this->issues()
200
        ->with('user', 'tags')
201
            ->where('status', '=', Project\Issue::STATUS_OPEN)
202
            ->whereIn('projects_issues_tags.tag_id', $tagIds)
203
            ->join('projects_issues_tags', 'issue_id', '=', 'id')
204
            ->orderBy('id')
205
            ->get()
206
            ->groupBy(function (Project\Issue $issue) use ($tagIds) {
207
                return $issue->tags->last()->name;
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Tinyissue\Model\Project\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
208
            });
209
210
        return $issues;
211
    }
212
213
    /**
214
     * Returns users assigned to the project that can fix issues (with edit permission).
215
     *
216
     * @return Relations\BelongsToMany
217
     */
218 7
    public function usersCanFixIssue()
219
    {
220 7
        return $this->users()->where('users.role_id', '>', 1);
221
    }
222
}
223