Passed
Push — master ( 4708a1...2e0f39 )
by Younes
02:12
created

VoteBuilder::voteUp()   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 0
1
<?php
2
3
namespace Yoeunes\Voteable;
4
5
use Yoeunes\Voteable\Models\Vote;
6
use Yoeunes\Voteable\Traits\Voteable;
7
use Illuminate\Database\Eloquent\Model;
8
use Yoeunes\Voteable\Exceptions\EmptyUser;
9
use Yoeunes\Voteable\Exceptions\UserDoestNotHaveID;
10
use Illuminate\Database\Eloquent\Relations\Relation;
11
use Yoeunes\Voteable\Exceptions\VoteableModelNotFound;
12
use Yoeunes\Voteable\Exceptions\ModelDoesNotUseVoteableTrait;
13
14
class VoteBuilder
15
{
16
    protected $user;
17
18
    protected $voteable;
19
20
    protected $uniqueVoteForUsers = true;
21
22
    public function __construct()
23
    {
24
        if (config('voteable.auth_user')) {
25
            $this->user = auth()->id();
0 ignored issues
show
Bug introduced by
The method id does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
26
        }
27
28
        if (config('voteable.user_vote_once')) {
29
            $this->uniqueVoteForUsers = true;
30
        }
31
    }
32
33
    /**
34
     * @param Model|int $user
35
     *
36
     * @return VoteBuilder
37
     *
38
     * @throws \Throwable
39
     */
40
    public function user($user)
41
    {
42
        throw_if($user instanceof Model && empty($user->id), UserDoestNotHaveID::class, 'User object does not have ID');
43
44
        $this->user = $user instanceof Model ? $user->id : $user;
45
46
        return $this;
47
    }
48
49
    /**
50
     * @param Model $voteable
51
     *
52
     * @return VoteBuilder
53
     *
54
     * @throws \Throwable
55
     */
56
    public function voteable(Model $voteable)
57
    {
58
        throw_unless(in_array(Voteable::class, class_uses_recursive($voteable)), ModelDoesNotUseVoteableTrait::class, get_class($voteable).' does not use the Voteable Trait');
59
60
        $this->voteable = $voteable;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param bool $unique
67
     *
68
     * @return VoteBuilder
69
     */
70
    public function uniqueVoteForUsers(bool $unique)
71
    {
72
        $this->uniqueVoteForUsers = $unique;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param int $amount
79
     *
80
     * @return Vote
81
     *
82
     * @throws \Throwable
83
     */
84
    public function amount(int $amount)
85
    {
86
        throw_if(empty($this->user), EmptyUser::class, 'Empty user');
87
88
        throw_if(empty($this->voteable->id), VoteableModelNotFound::class, 'Voteable model not found');
89
90
        $data = [
91
            'user_id'       => $this->user,
92
            'voteable_id'   => $this->voteable->id,
93
            'voteable_type' => Relation::getMorphedModel(get_class($this->voteable)) ?? get_class($this->voteable),
94
        ];
95
96
        $vote = $this->uniqueVoteForUsers ? Vote::firstOrNew($data) : (new Vote())->fill($data);
97
98
        $vote->amount = $amount;
99
100
        $vote->save();
101
102
        return $vote;
103
    }
104
105
    /**
106
     * @return Vote
107
     *
108
     * @throws \Throwable
109
     */
110
    public function voteUp()
111
    {
112
        return $this->amount(config('voteable.amount.up'));
113
    }
114
115
    /**
116
     * @return Vote
117
     *
118
     * @throws \Throwable
119
     */
120
    public function voteDown()
121
    {
122
        return $this->amount(config('voteable.amount.down'));
123
    }
124
}
125