Test Failed
Push — master ( 941391...ba1259 )
by Younes
01:37
created

Voteable::updateVotesForUser()   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 2
1
<?php
2
3
namespace Yoeunes\Voteable\Traits;
4
5
use Yoeunes\Voteable\Models\Vote;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Query\JoinClause;
9
use Illuminate\Database\Eloquent\Relations\Relation;
10
11
trait Voteable
12
{
13
    /**
14
     * This model has many votes.
15
     *
16
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
17
     */
18
    public function votes()
19
    {
20
        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...
21
    }
22
23
    /**
24
     * @return mixed
25
     */
26
    public function upVotesCount()
27
    {
28
        return $this->votes()->where('score', '>=', 0)->sum('score');
29
    }
30
31
    /**
32
     * @return mixed
33
     */
34
    public function downVotesCount()
35
    {
36
        return $this->votes()->where('score', '<', 0)->sum('score');
37
    }
38
39
    /**
40
     * @return bool
41
     */
42
    public function isUpVotes()
43
    {
44
        return $this->votes()->where('score', '>=', 0)->exists();
45
    }
46
47
    /**
48
     * @return bool
49
     */
50
    public function isDownVoted()
51
    {
52
        return $this->votes()->where('score', '<', 0)->exists();
53
    }
54
55
    /**
56
     * @param int $user_id
57
     *
58
     * @return bool
59
     */
60
    public function isUpVotedByUser(int $user_id)
61
    {
62
        return $this->votes()->where('score', '>=', 0)->where('user_id', $user_id)->exists();
63
    }
64
65
    /**
66
     * @param int $user_id
67
     *
68
     * @return bool
69
     */
70
    public function isDownVotedByUser(int $user_id)
71
    {
72
        return $this->votes()->where('score', '<', 0)->where('user_id', $user_id)->exists();
73
    }
74
75
    /**
76
     * @param Builder $query
77
     * @param string $direction
78
     * @param string $type
79
     *
80
     * @return Builder
81
     */
82
    public function scopeOrderByUpVotes(Builder $query, string $direction = 'asc', string $type = '>=')
83
    {
84
        return $query
85
            ->leftJoin('votes', function (JoinClause $join) {
86
                $join
87
                    ->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...
88
                    ->where('votes.voteable_type', Relation::getMorphedModel(__CLASS__) ?? __CLASS__);
89
            })
90
            ->where('score', $type, 0)
91
            ->addSelect(DB::raw('SUM(votes.value) as count_votes'))
92
            ->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...
93
            ->orderBy('count_votes', $direction);
94
    }
95
96
    /**
97
     * @param Builder $query
98
     * @param string $direction
99
     *
100
     * @return Builder
101
     */
102
    public function scopeOrderByDownVotes(Builder $query, string $direction = 'asc')
103
    {
104
        return $this->scopeOrderByUpVotes($query, $direction, '<');
105
    }
106
107
    /**
108
     * @param int $vote_id
109
     *
110
     * @return mixed
111
     */
112
    public function deleteVote(int $vote_id)
113
    {
114
        return $this->votes()->where('id', $vote_id)->delete();
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120
    public function resetVotes()
121
    {
122
        return $this->votes()->delete();
123
    }
124
125
    /**
126
     * @param int $user_id
127
     *
128
     * @return mixed
129
     */
130
    public function deleteVotesForUser(int $user_id)
131
    {
132
        return $this->votes()->where('user_id', $user_id)->delete();
133
    }
134
135
    /**
136
     * @param int $user_id
137
     * @param int $score
138
     *
139
     * @return int
140
     */
141
    public function updateVotesForUser(int $user_id, int $score)
142
    {
143
        return $this->votes()->where('user_id', $user_id)->update(['score' => $score]);
144
    }
145
146
    /**
147
     * @param int $vote_id
148
     * @param int $score
149
     *
150
     * @return int
151
     */
152
    public function updateRating(int $vote_id, int $score)
153
    {
154
        return $this->votes()->where('id', $vote_id)->update(['score' => $score]);
155
    }
156
}
157