Test Failed
Push — master ( cad3a8...9b55cf )
by Younes
02:30
created

Voteable::isUnlikedByUser()   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
namespace Yoeunes\Voteable\Traits;
4
5
use Yoeunes\Rateable\VoteBuilder;
6
use Yoeunes\Voteable\Models\Vote;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Query\JoinClause;
10
use Illuminate\Database\Eloquent\Relations\Relation;
11
12
trait Voteable
13
{
14
    /**
15
     * This model has many votes.
16
     *
17
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
18
     */
19
    public function votes()
20
    {
21
        return $this->morphMany(Vote::class, 'voteable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
22
    }
23
24
    /**
25
     * @return mixed
26
     */
27
    public function upVotesCount()
28
    {
29
        return $this->votes()->where('score', '>=', 0)->sum('score');
30
    }
31
32
    public function likesCount()
33
    {
34
        return $this->upVotesCount();
35
    }
36
37
    /**
38
     * @return mixed
39
     */
40
    public function downVotesCount()
41
    {
42
        return $this->votes()->where('score', '<', 0)->sum('score');
43
    }
44
45
    public function dislikesCount()
46
    {
47
        return $this->downVotesCount();
48
    }
49
50
    public function unlikesCount()
51
    {
52
        return $this->downVotesCount();
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    public function isUpVoted()
59
    {
60
        return $this->votes()->where('score', '>=', 0)->exists();
61
    }
62
63
    public function isLiked()
64
    {
65
        return $this->isUpVoted();
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    public function isDownVoted()
72
    {
73
        return $this->votes()->where('score', '<', 0)->exists();
74
    }
75
76
    /**
77
     * @param int $user_id
78
     *
79
     * @return bool
80
     */
81
    public function isUpVotedByUser(int $user_id)
82
    {
83
        return $this->votes()->where('score', '>=', 0)->where('user_id', $user_id)->exists();
84
    }
85
86
    public function isLikedByUser(int $user_id)
87
    {
88
        return $this->isUpVotedByUser($user_id);
89
    }
90
91
    /**
92
     * @param int $user_id
93
     *
94
     * @return bool
95
     */
96
    public function isDownVotedByUser(int $user_id)
97
    {
98
        return $this->votes()->where('score', '<', 0)->where('user_id', $user_id)->exists();
99
    }
100
101
    public function isDislikedByUser(int $user_id)
102
    {
103
        return $this->isDownVotedByUser($user_id);
104
    }
105
106
    public function isUnlikedByUser(int $user_id)
107
    {
108
        return $this->isDownVotedByUser($user_id);
109
    }
110
111
    /**
112
     * @param Builder $query
113
     * @param string $direction
114
     * @param string $type
115
     *
116
     * @return Builder
117
     */
118
    public function scopeOrderByUpVotes(Builder $query, string $direction = 'asc', string $type = '>=')
119
    {
120
        return $query
121
            ->leftJoin('votes', function (JoinClause $join) {
122
                $join
123
                    ->on('votes.voteable_id', $this->getTable() . '.id')
0 ignored issues
show
Bug introduced by
It seems like getTable() 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...
124
                    ->where('votes.voteable_type', Relation::getMorphedModel(__CLASS__) ?? __CLASS__);
125
            })
126
            ->where('score', $type, 0)
127
            ->addSelect(DB::raw('SUM(votes.value) as count_votes'))
128
            ->groupBy($this->getTable(). '.id')
0 ignored issues
show
Bug introduced by
It seems like getTable() 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...
129
            ->orderBy('count_votes', $direction);
130
    }
131
132
    public function scopeOrderByLikes(Builder $query, string $direction = 'asc', string $type = '>=')
133
    {
134
        return $this->scopeOrderByUpVotes($query, $direction, $type);
135
    }
136
137
    /**
138
     * @param Builder $query
139
     * @param string $direction
140
     *
141
     * @return Builder
142
     */
143
    public function scopeOrderByDownVotes(Builder $query, string $direction = 'asc')
144
    {
145
        return $this->scopeOrderByUpVotes($query, $direction, '<');
146
    }
147
148
    public function scopeOrderByDislikes(Builder $query, string $direction = 'asc')
149
    {
150
        return $this->scopeOrderByDownVotes($query, $direction);
151
    }
152
153
    /**
154
     * @param int $vote_id
155
     *
156
     * @return mixed
157
     */
158
    public function deleteVote(int $vote_id)
159
    {
160
        return $this->votes()->where('id', $vote_id)->delete();
161
    }
162
163
    public function deleteLike(int $like_id)
164
    {
165
        return $this->deleteVote($like_id);
166
    }
167
168
    /**
169
     * @return mixed
170
     */
171
    public function resetVotes()
172
    {
173
        return $this->votes()->delete();
174
    }
175
176
    public function resetLikes()
177
    {
178
        return $this->resetVotes();
179
    }
180
181
    /**
182
     * @param int $user_id
183
     *
184
     * @return mixed
185
     */
186
    public function deleteVotesForUser(int $user_id)
187
    {
188
        return $this->votes()->where('user_id', $user_id)->delete();
189
    }
190
191
    public function deleteLikesForUser(int $user_id)
192
    {
193
        return $this->deleteVotesForUser($user_id);
194
    }
195
196
    /**
197
     * @param int $user_id
198
     * @param int $score
199
     *
200
     * @return int
201
     */
202
    public function updateVotesForUser(int $user_id, int $score)
203
    {
204
        return $this->votes()->where('user_id', $user_id)->update(['score' => $score]);
205
    }
206
207
    public function updateLikesForUser(int $user_id, int $score)
208
    {
209
        return $this->updateVotesForUser($user_id, $score);
210
    }
211
212
    /**
213
     * @param int $vote_id
214
     * @param int $score
215
     *
216
     * @return int
217
     */
218
    public function updateVote(int $vote_id, int $score)
219
    {
220
        return $this->votes()->where('id', $vote_id)->update(['score' => $score]);
221
    }
222
223
    public function updateLike(int $vote_id, int $score)
224
    {
225
        return $this->updateVote($vote_id, $score);
226
    }
227
228
    /**
229
     * @return VoteBuilder
230
     *
231
     * @throws \Throwable
232
     */
233
    public function getRatingBuilder()
234
    {
235
        return (new VoteBuilder())
236
            ->voteable($this);
237
    }
238
}
239