Passed
Push — master ( 4a474d...df1b75 )
by Stephen
02:14
created

UserBuilder::whereUsername()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Sfneal\Users\Builders;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Query\Builder;
8
use Sfneal\Builders\QueryBuilder;
9
use Sfneal\Users\Builders\Interfaces\WhereUserInterface;
10
use Sfneal\Users\Models\User;
11
use Sfneal\Users\Scopes\UserActiveScope;
12
13
class UserBuilder extends QueryBuilder implements WhereUserInterface
14
{
15
    /**
16
     * @var string MySQL select objects to be queried in a raw json return
17
     */
18
    protected $selectRawJson;
19
20
    /**
21
     * @var Model|User
22
     */
23
    protected $targetModel = User::class;
24
25
    /**
26
     * UserBuilder constructor.
27
     *
28
     * @param Builder $query
29
     */
30
    public function __construct(Builder $query)
31
    {
32
        parent::__construct($query);
33
        $this->setSelectRawJson();
34
    }
35
36
    /**
37
     * Retrieve the User model's 'name' attribute by concatenating first and last name columns.
38
     *
39
     * @return string
40
     */
41
    private function concatName(): string
42
    {
43
        return $this->concatColumns('first_name', 'last_name');
44
    }
45
46
    /**
47
     * Retrieve the User model's 'name' attribute by concatenating nickname and last name columns.
48
     *
49
     * @return string
50
     */
51
    private function concatNickname(): string
52
    {
53
        return $this->concatColumns('nickname', 'last_name');
54
    }
55
56
    /**
57
     * Dynamically set the $selectRawJson.
58
     *
59
     * @return void
60
     */
61
    private function setSelectRawJson(): void
62
    {
63
        $raw = "{$this->tableName}.{$this->primaryKeyName} as id, ";
64
        $raw .= $this->ifStatement(
65
            "nickname is not null and {$this->tableName}.nickname_preferred=1",
66
            $this->concatNickname(),
67
            $this->concatName()
68
        );
69
        $raw .= ' as text';
70
        $this->selectRawJson = $raw;
71
    }
72
73
    /**
74
     * Find a User.
75
     *
76
     * @param int $user_id
77
     *
78
     * @return $this
79
     */
80
    public function whereUser(int $user_id)
81
    {
82
        $this->where('id', '=', $user_id);
83
84
        return $this;
85
    }
86
87
    /**
88
     * Scope query to activity that was performed by any of the specified users.
89
     *
90
     * @param array $user_ids
91
     *
92
     * @return $this|WhereUserInterface
93
     */
94
    public function whereUserIn(array $user_ids)
95
    {
96
        $this->whereIn('id', $user_ids);
0 ignored issues
show
Bug introduced by
The method whereIn() does not exist on Sfneal\Users\Builders\UserBuilder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        $this->/** @scrutinizer ignore-call */ 
97
               whereIn('id', $user_ids);
Loading history...
97
98
        return $this;
99
    }
100
101
    /**
102
     * Scope results to User's with a certain username.
103
     *
104
     * @param $value
105
     *
106
     * @return $this
107
     */
108
    public function whereUsername($value)
109
    {
110
        $this->where('username', '=', $value);
111
112
        return $this;
113
    }
114
115
    /**
116
     * Scope a query to User's with names starting with a particular string.
117
     *
118
     * @param string $name
119
     *
120
     * @return $this
121
     */
122
    public function whereNameLike(string $name)
123
    {
124
        // Full name like $name
125
        $this->whereNameLikeRaw($name);
126
127
        // Has nickname & nickname like $name
128
        $this->orWhere(function (self $builder) use ($name) {
129
            $builder->whereNotNull('nickname');
0 ignored issues
show
Bug introduced by
The method whereNotNull() does not exist on Sfneal\Users\Builders\UserBuilder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
            $builder->/** @scrutinizer ignore-call */ 
130
                      whereNotNull('nickname');
Loading history...
130
            $builder->whereNameLikeRaw($name, $this->concatNickname());
131
        });
132
133
        return $this;
134
    }
135
136
    /**
137
     * Add a whereNameLike scope with custom columns (like concatenated).
138
     *
139
     * @param string      $name
140
     * @param string|null $column
141
     *
142
     * @return $this
143
     */
144
    private function whereNameLikeRaw(string $name, string $column = null)
145
    {
146
        // Use concatName method if no $column was provided
147
        $this->whereRaw(($column ?? $this->concatName())." LIKE '%{$name}%'");
0 ignored issues
show
Bug introduced by
The method whereRaw() does not exist on Sfneal\Users\Builders\UserBuilder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
        $this->/** @scrutinizer ignore-call */ 
148
               whereRaw(($column ?? $this->concatName())." LIKE '%{$name}%'");
Loading history...
148
149
        return $this;
150
    }
151
152
    /**
153
     * Active Users.
154
     *
155
     * @param int $value
156
     *
157
     * @return $this
158
     */
159
    public function whereActive($value = 1)
160
    {
161
        $this->where('status', '=', $value);
162
163
        return $this;
164
    }
165
166
    /**
167
     * Inactive Users.
168
     *
169
     * @return $this
170
     */
171
    public function whereInactive()
172
    {
173
        $this->withInactive();
174
        $this->whereActive(0);
175
176
        return $this;
177
    }
178
179
    /**
180
     * Include 'inactive' users in collection.
181
     *
182
     * @return $this
183
     */
184
    public function withInactive()
185
    {
186
        $this->withoutGlobalScope(UserActiveScope::class);
187
188
        return $this;
189
    }
190
191
    /**
192
     * Retrieve all Users regardless of 'active status'.
193
     *
194
     * @param array $columns
195
     *
196
     * @return Collection
197
     */
198
    public function allWithInactive($columns = ['*'])
199
    {
200
        return $this->withInactive()->get($columns);
201
    }
202
}
203