Completed
Push — master ( 78665f...8b7dd5 )
by Carlos
11s
created

CanVote::upvotes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/laravel-follow
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\LaravelFollow\Traits;
13
14
use Overtrue\LaravelFollow\Follow;
15
16
/**
17
 * Trait CanVote.
18
 */
19
trait CanVote
20
{
21
    /**
22
     * Vote an item or items.
23
     *
24
     * @param int|array|\Illuminate\Database\Eloquent\Model $targets
25
     * @param string                                        $type
26
     * @param string                                        $class
27
     *
28
     * @return array
29
     */
30
    public function vote($targets, $type = 'upvote', $class = __CLASS__)
31
    {
32
	    $this->cancelVote($targets);
33
    	
34
        return Follow::attachRelations($this, str_plural($type), $targets, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanVote is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Overtrue\LaravelFollow\Follow::attachRelations(). ( Ignorable by Annotation )

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

34
        return Follow::attachRelations(/** @scrutinizer ignore-type */ $this, str_plural($type), $targets, $class);
Loading history...
35
    }
36
	
37
	/**
38
	 * Upvote an item or items.
39
	 *
40
	 * @param int|array|\Illuminate\Database\Eloquent\Model $targets
41
	 * @param string                                        $type
42
	 * @param string                                        $class
43
	 *
44
	 * @return array
45
	 */
46
	public function upvote($targets, $class = __CLASS__)
47
	{
48
		return $this->vote($targets, 'upvote', $class);
49
	}
50
	
51
	/**
52
	 * Downvote an item or items.
53
	 *
54
	 * @param int|array|\Illuminate\Database\Eloquent\Model $targets
55
	 * @param string                                        $type
56
	 * @param string                                        $class
57
	 *
58
	 * @return array
59
	 */
60
	public function downvote($targets, $class = __CLASS__)
61
	{
62
		return $this->vote($targets, 'downvote', $class);
63
	}
64
65
    /**
66
     * Cancel vote for an item or items.
67
     *
68
     * @param int|array|\Illuminate\Database\Eloquent\Model $targets
69
     * @param string                                        $class
70
     *
71
     * @return array
72
     */
73
    public function cancelVote($targets, $class = __CLASS__)
74
    {
75
        Follow::detachRelations($this, 'upvotes', $targets, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanVote is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Overtrue\LaravelFollow\Follow::detachRelations(). ( Ignorable by Annotation )

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

75
        Follow::detachRelations(/** @scrutinizer ignore-type */ $this, 'upvotes', $targets, $class);
Loading history...
76
        Follow::detachRelations($this, 'downvotes', $targets, $class);
77
	
78
	    return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Overtrue\LaravelFollow\Traits\CanVote which is incompatible with the documented return type array.
Loading history...
79
    }
80
81
    /**
82
     * Check if user is upvoted given item.
83
     *
84
     * @param int|array|\Illuminate\Database\Eloquent\Model $target
85
     * @param string                                        $class
86
     *
87
     * @return bool
88
     */
89
    public function hasUpvoted($target, $class = __CLASS__)
90
    {
91
        return Follow::isRelationExists($this, 'upvote', $target, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanVote is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Overtrue\LaravelFollow\Follow::isRelationExists(). ( Ignorable by Annotation )

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

91
        return Follow::isRelationExists(/** @scrutinizer ignore-type */ $this, 'upvote', $target, $class);
Loading history...
92
    }
93
	
94
	/**
95
	 * Check if user is downvoted given item.
96
	 *
97
	 * @param int|array|\Illuminate\Database\Eloquent\Model $target
98
	 * @param string                                        $class
99
	 *
100
	 * @return bool
101
	 */
102
	public function hasDownvoted($target, $class = __CLASS__)
103
	{
104
		return Follow::isRelationExists($this, 'downvote', $target, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanVote is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Overtrue\LaravelFollow\Follow::isRelationExists(). ( Ignorable by Annotation )

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

104
		return Follow::isRelationExists(/** @scrutinizer ignore-type */ $this, 'downvote', $target, $class);
Loading history...
105
	}
106
	
107
	/**
108
	 * Return item votes.
109
	 *
110
	 * @param string $class
111
	 *
112
	 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
113
	 */
114
	public function votes($class = __CLASS__)
115
	{
116
		return $this->morphedByMany($class, config('follow.morph_prefix'), config('follow.followable_table'))
0 ignored issues
show
Bug introduced by
It seems like morphedByMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

116
		return $this->/** @scrutinizer ignore-call */ morphedByMany($class, config('follow.morph_prefix'), config('follow.followable_table'))
Loading history...
117
					->wherePivotIn('relation', [Follow::RELATION_UPVOTE, Follow::RELATION_DOWNVOTE])
118
		            ->withPivot('followable_type', 'relation', 'created_at');
119
	}
120
121
    /**
122
     * Return item upvotes.
123
     *
124
     * @param string $class
125
     *
126
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
127
     */
128
    public function upvotes($class = __CLASS__)
129
    {
130
        return $this->morphedByMany($class, config('follow.morph_prefix'), config('follow.followable_table'))
131
                    ->wherePivot('relation', '=', Follow::RELATION_UPVOTE)
132
                    ->withPivot('followable_type', 'relation', 'created_at');
133
    }
134
	
135
	/**
136
	 * Return item downvotes.
137
	 *
138
	 * @param string $class
139
	 *
140
	 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
141
	 */
142
	public function downvotes($class = __CLASS__)
143
	{
144
		return $this->morphedByMany($class, config('follow.morph_prefix'), config('follow.followable_table'))
145
		            ->wherePivot('relation', '=', Follow::RELATION_DOWNVOTE)
146
		            ->withPivot('followable_type', 'relation', 'created_at');
147
	}
148
}
149