Test Failed
Push — master ( 3baf57...0bc6fd )
by Younes
01:42
created

VoteBuilder::amount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Yoeunes\Rateable;
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
    /**
23
     * @param Model|int $user
24
     *
25
     * @return VoteBuilder
26
     *
27
     * @throws \Throwable
28
     */
29
    public function user($user)
30
    {
31
        throw_if($user instanceof Model && empty($user->id), UserDoestNotHaveID::class, 'User object does not have ID');
32
33
        $this->user = $user instanceof Model ? $user->id : $user;
34
35
        return $this;
36
    }
37
38
    /**
39
     * @param Model $voteable
40
     *
41
     * @return VoteBuilder
42
     *
43
     * @throws \Throwable
44
     */
45
    public function voteable(Model $voteable)
46
    {
47
        throw_unless(in_array(Voteable::class, class_uses_recursive($voteable)), ModelDoesNotUseVoteableTrait::class, get_class($voteable).' does not use the Voteable Trait');
48
49
        $this->voteable = $voteable;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param bool $unique
56
     *
57
     * @return VoteBuilder
58
     */
59
    public function uniqueVoteForUsers(bool $unique)
60
    {
61
        $this->uniqueVoteForUsers = $unique;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param int $amount
68
     *
69
     * @return Vote
70
     *
71
     * @throws \Throwable
72
     */
73
    public function amount(int $amount)
74
    {
75
        throw_if(empty($this->user), EmptyUser::class, 'Empty user');
76
77
        throw_if(empty($this->voteable->id), VoteableModelNotFound::class, 'Voteable model not found');
78
79
        $data = [
80
            'user_id'       => $this->user,
81
            'voteable_id'   => $this->voteable->id,
82
            'voteable_type' => Relation::getMorphedModel(get_class($this->voteable)) ?? get_class($this->voteable),
83
        ];
84
85
        $vote = $this->uniqueVoteForUsers ? Vote::firstOrNew($data) : (new Vote())->fill($data);
86
87
        $vote->amount = $amount;
88
89
        $vote->save();
90
91
        return $vote;
92
    }
93
94
    /**
95
     * @return Vote
96
     *
97
     * @throws \Throwable
98
     */
99
    public function voteUp()
100
    {
101
        return $this->amount(config('voteable.amount.up'));
102
    }
103
104
    /**
105
     * @return Vote
106
     *
107
     * @throws \Throwable
108
     */
109
    public function voteDown()
110
    {
111
        return $this->amount(config('voteable.amount.down'));
112
    }
113
}
114