Completed
Branch develop-3.0 (ae28cb)
by Mohamed
08:28
created

IssueScopes::scopeOpen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\Project;
13
14
use Illuminate\Database\Eloquent;
15
use Illuminate\Database\Eloquent\Builder;
16
use Tinyissue\Contracts\Model\UserInterface;
17
use Tinyissue\Model;
18
19
/**
20
 * IssueScopes is trait class containing the model scope methods.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 *
24
 * @property static $this
25
 */
26
trait IssueScopes
27
{
28
    /**
29
     * Filter status by open status.
30
     *
31
     * @param Builder $query
32
     *
33
     * @return Builder
34
     */
35
    public function scopeOpen(Builder $query)
36
    {
37
        return $this->scopeStatus($query, Issue::STATUS_OPEN);
38
    }
39
40
    /**
41
     * Filter status by closed status.
42
     *
43
     * @param Builder $query
44
     *
45
     * @return Builder
46
     */
47
    public function scopeClosed(Builder $query)
48
    {
49
        return $this->scopeStatus($query, Issue::STATUS_CLOSED);
50
    }
51
52
    /**
53
     * Filter status by a status.
54
     *
55
     * @param Builder $query
56
     * @param int     $status
57
     *
58
     * @return Builder
59
     */
60
    public function scopeStatus(Builder $query, $status = Issue::STATUS_OPEN)
61
    {
62
        return $query->where('status', '=', $status);
63
    }
64
65
    /**
66
     * Filter issue by created by or assigned to field based on user role.
67
     *
68
     * @param Builder            $query
69
     * @param UserInterface|null $user
70
     *
71
     * @return Builder
72
     */
73 View Code Duplication
    public function scopeAssignedOrCreated(Builder $query, UserInterface $user = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
74
    {
75
        if ($user instanceof UserInterface && $user->isUser()) {
76
            return $this->scopeCreatedBy($query, $user);
77
        }
78
79
        return $this->scopeAssignedTo($query, $user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by parameter $user on line 73 can also be of type object<Tinyissue\Contracts\Model\UserInterface>; however, Tinyissue\Model\Project\...opes::scopeAssignedTo() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
80
    }
81
82
    /**
83
     * Filter issue by assigned to.
84
     *
85
     * @param Builder $query
86
     * @param null    $user
87
     *
88
     * @return Builder
89
     */
90
    public function scopeAssignedTo(Builder $query, $user = null)
91
    {
92
        return $this->whereIdEqual($query, 'assigned_to', $user);
93
    }
94
95
    /**
96
     * Filter issue by created by.
97
     *
98
     * @param Builder            $query
99
     * @param UserInterface|null $user
100
     *
101
     * @return Builder
102
     */
103
    public function scopeCreatedBy(Builder $query, UserInterface $user = null)
104
    {
105
        return $this->whereIdEqual($query, 'created_by', $user);
0 ignored issues
show
Documentation introduced by
$user is of type null|object<Tinyissue\Co...ts\Model\UserInterface>, but the function expects a integer|object<Illuminat...atabase\Eloquent\Model>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
    }
107
108
    /**
109
     * Filter issue by created by if user logged in as User role adn current project is private internal.
110
     *
111
     * @param Builder            $query
112
     * @param Model\Project      $project
113
     * @param UserInterface|null $user
114
     *
115
     * @return Builder
116
     */
117 View Code Duplication
    public function scopeLimitByCreatedForInternalProject(Builder $query, Model\Project $project, UserInterface $user = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
118
    {
119
        if ($user && $user->isUser() && $project->isPrivateInternal()) {
120
            $query = $this->scopeCreatedBy($query, $user);
121
        }
122
123
        return $query;
124
    }
125
126
    /**
127
     * Filter issue by project id.
128
     *
129
     * @param Builder $query
130
     * @param int     $projectId
131
     *
132
     * @return Builder
133
     */
134
    public function scopeForProject(Builder $query, $projectId)
135
    {
136
        return $this->whereIdEqual($query, 'project_id', $projectId);
137
    }
138
139
    /**
140
     * Helper method to filter by a field and a value.
141
     *
142
     * @param Builder            $query
143
     * @param string             $field
144
     * @param int|Eloquent\Model $idOrObject
145
     *
146
     * @return $this|Builder
147
     */
148
    protected function whereIdEqual(Builder $query, $field, $idOrObject)
149
    {
150
        $id = $idOrObject instanceof Eloquent\Model ? $idOrObject->id : $idOrObject;
151
        if ($id > 0) {
152
            return $query->where($field, '=', $id);
153
        }
154
155
        return $query;
156
    }
157
158
    /**
159
     * Filter with like %% on issue string fields title/body.
160
     *
161
     * @param Builder $query
162
     * @param string  $keyword
163
     *
164
     * @return Builder
165
     */
166
    public function scopeSearchContent(Builder $query, $keyword)
167
    {
168
        if (!empty($keyword)) {
169
            return $query->where(function (Builder $query) use ($keyword) {
170
                $query->where('title', 'like', '%' . $keyword . '%');
171
                $query->orWhere('body', 'like', '%' . $keyword . '%');
172
            });
173
        }
174
175
        return $query;
176
    }
177
178
    /**
179
     * Filter issues by an array of tag ids.
180
     *
181
     * @param Builder $query
182
     * @param array   ...$tags
183
     *
184
     * @return Builder
185
     */
186
    public function scopeWhereTags(Builder $query, ...$tags)
187
    {
188
        $tags = array_filter($tags);
189
190
        if (!empty($tags)) {
191
            $query->whereHas('tags', function (Builder $query) use ($tags) {
192
                $query->whereIn('id', $tags);
193
            });
194
        }
195
196
        return $query;
197
    }
198
}
199