Completed
Push — master ( 896b70...2ed772 )
by Younes
07:16
created

RatingBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A user() 0 8 3
A rateable() 0 8 1
A uniqueRatingForUsers() 0 6 1
B rate() 0 24 3
1
<?php
2
3
namespace Yoeunes\Rateable\Builders;
4
5
use Yoeunes\Rateable\Models\Rating;
6
use Yoeunes\Rateable\Traits\Rateable;
7
use Illuminate\Database\Eloquent\Model;
8
use Yoeunes\Rateable\Exceptions\EmptyUser;
9
use Yoeunes\Rateable\Exceptions\InvalidRatingValue;
10
use Yoeunes\Rateable\Exceptions\UserDoestNotHaveID;
11
use Yoeunes\Rateable\Exceptions\RateableModelNotFound;
12
use Yoeunes\Rateable\Exceptions\ModelDoesNotUseRateableTrait;
13
14
class RatingBuilder
15
{
16
    protected $user;
17
18
    protected $rateable;
19
20
    protected $uniqueRatingForUsers = true;
21
22
    public function __construct()
23
    {
24
        if (config('rateable.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('rateable.user_rate_once')) {
29
            $this->uniqueVoteForUsers = true;
0 ignored issues
show
Bug introduced by
The property uniqueVoteForUsers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
        }
31
    }
32
33
    /**
34
     * @param Model|int $user
35
     *
36
     * @return RatingBuilder
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 $rateable
51
     *
52
     * @return $this
53
     *
54
     * @throws \Throwable
55
     */
56
    public function rateable(Model $rateable)
57
    {
58
        throw_unless(in_array(Rateable::class, class_uses_recursive($rateable)), ModelDoesNotUseRateableTrait::class, get_class($rateable) . ' does not use the Rateable Trait');
59
60
        $this->rateable = $rateable;
61
62
        return $this;
63
    }
64
65
    public function uniqueRatingForUsers(bool $unique)
66
    {
67
        $this->uniqueRatingForUsers = $unique;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @param int $value
74
     *
75
     * @return Rating
76
     *
77
     * @throws \Throwable
78
     */
79
    public function rate(int $value)
80
    {
81
        throw_if($value < config('rateable.min_rating') || $value > config('rateable.max_rating'), InvalidRatingValue::class, 'Invalid rating value');
82
83
        throw_if(empty($this->user), EmptyUser::class, 'Empty user');
84
85
        throw_if(empty($this->rateable->id), RateableModelNotFound::class, 'Rateable model not found');
86
87
        $data = [
88
            'user_id'       => $this->user,
89
            'rateable_id'   => $this->rateable->id,
90
            'rateable_type' => morph_type($this->rateable),
91
        ];
92
93
        $ratingModel = config('rateable.rating');
94
95
        $rating = $this->uniqueRatingForUsers ? (new $ratingModel)->firstOrNew($data) : (new $ratingModel)->fill($data);
96
97
        $rating->value = $value;
98
99
        $rating->save();
100
101
        return $rating;
102
    }
103
}
104