Completed
Push — master ( 957e25...61e39d )
by Marcel
02:22
created

Voucher::message()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace BeyondCode\Vouchers\Rules;
4
5
use Vouchers;
6
use Illuminate\Contracts\Validation\Rule;
7
use BeyondCode\Vouchers\Exceptions\VoucherExpired;
8
use BeyondCode\Vouchers\Exceptions\VoucherIsInvalid;
9
use BeyondCode\Vouchers\Exceptions\VoucherAlreadyRedeemed;
10
11
class Voucher implements Rule
12
{
13
    protected $isInvalid = false;
14
    protected $isExpired = false;
15
    protected $wasRedeemd = false;
16
17
    /**
18
     * Determine if the validation rule passes.
19
     *
20
     * @param  string $attribute
21
     * @param  mixed $value
22
     * @return bool
23
     */
24
    public function passes($attribute, $value)
25
    {
26
        try {
27
            $voucher = Vouchers::check($value);
28
29
            // Check if the voucher was already redeemed
30
            if (auth()->check() && $voucher->users()->wherePivot('user_id', auth()->id())->exists()) {
0 ignored issues
show
Bug introduced by
The method check 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...
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...
31
                throw VoucherAlreadyRedeemed::create($voucher);
32
            }
33
        } catch (VoucherIsInvalid $exception) {
34
            $this->isInvalid = true;
35
            return false;
36
        } catch (VoucherExpired $exception) {
37
            $this->isExpired = true;
38
            return false;
39
        } catch (VoucherAlreadyRedeemed $exception) {
40
            $this->wasRedeemd = true;
41
            return false;
42
        }
43
44
        return true;
45
    }
46
47
    /**
48
     * Get the validation error message.
49
     *
50
     * @return string
51
     */
52
    public function message()
53
    {
54
        if ($this->wasRedeemd) {
55
            return trans('vouchers::validation.code_redeemed');
56
        }
57
        if ($this->isExpired) {
58
            return trans('vouchers::validation.code_expired');
59
        }
60
        return trans('vouchers::validation.code_invalid');
61
    }
62
}