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

CanBeVoted   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 72
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isUpvotedBy() 0 3 1
A downvoters() 0 5 1
A isDownvotedBy() 0 3 1
A isVotedBy() 0 3 1
A voters() 0 5 1
A upvoters() 0 5 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 CanBeVoted.
18
 */
19
trait CanBeVoted
20
{
21
    /**
22
     * Check if item is voted by given user.
23
     *
24
     * @param int $user
25
     *
26
     * @return bool
27
     */
28
    public function isVotedBy($user, $type = null)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

28
    public function isVotedBy($user, /** @scrutinizer ignore-unused */ $type = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
    {
30
        return Follow::isRelationExists($this, 'voters', $user);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanBeVoted 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

30
        return Follow::isRelationExists(/** @scrutinizer ignore-type */ $this, 'voters', $user);
Loading history...
31
    }
32
	
33
	/**
34
	 * Check if item is upvoted by given user.
35
	 *
36
	 * @param int $user
37
	 *
38
	 * @return bool
39
	 */
40
	public function isUpvotedBy($user)
41
	{
42
		return Follow::isRelationExists($this, 'upvoters', $user);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanBeVoted 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

42
		return Follow::isRelationExists(/** @scrutinizer ignore-type */ $this, 'upvoters', $user);
Loading history...
43
	}
44
	
45
	/**
46
	 * Check if item is downvoted by given user.
47
	 *
48
	 * @param int $user
49
	 *
50
	 * @return bool
51
	 */
52
	public function isDownvotedBy($user)
53
	{
54
		return Follow::isRelationExists($this, 'downvoters', $user);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanBeVoted 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

54
		return Follow::isRelationExists(/** @scrutinizer ignore-type */ $this, 'downvoters', $user);
Loading history...
55
	}
56
57
    /**
58
     * Return voters.
59
     *
60
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
61
     */
62
    public function voters()
63
    {
64
        return $this->morphToMany(config('follow.user_model'), config('follow.morph_prefix'), config('follow.followable_table'))
0 ignored issues
show
Bug introduced by
It seems like morphToMany() 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

64
        return $this->/** @scrutinizer ignore-call */ morphToMany(config('follow.user_model'), config('follow.morph_prefix'), config('follow.followable_table'))
Loading history...
65
                    ->wherePivotIn('relation', [Follow::RELATION_UPVOTE, Follow::RELATION_DOWNVOTE])
66
                    ->withPivot('followable_type', 'relation', 'created_at');
67
    }
68
	
69
	/**
70
	 * Return upvoters.
71
	 *
72
	 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
73
	 */
74
	public function upvoters()
75
	{
76
		return $this->morphToMany(config('follow.user_model'), config('follow.morph_prefix'), config('follow.followable_table'))
77
		            ->wherePivot('relation', '=', Follow::RELATION_UPVOTE)
78
		            ->withPivot('followable_type', 'relation', 'created_at');
79
	}
80
	
81
	/**
82
	 * Return downvoters.
83
	 *
84
	 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
85
	 */
86
	public function downvoters()
87
	{
88
		return $this->morphToMany(config('follow.user_model'), config('follow.morph_prefix'), config('follow.followable_table'))
89
		            ->wherePivot('relation', '=', Follow::RELATION_DOWNVOTE)
90
		            ->withPivot('followable_type', 'relation', 'created_at');
91
	}
92
}
93