CanRedeemVouchers   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A redeemCode() 0 19 3
A redeemVoucher() 0 4 1
A vouchers() 0 4 1
1
<?php
2
3
namespace BeyondCode\Vouchers\Traits;
4
5
use BeyondCode\Vouchers\Facades\Vouchers;
6
use BeyondCode\Vouchers\Models\Voucher;
7
use BeyondCode\Vouchers\Events\VoucherRedeemed;
8
use BeyondCode\Vouchers\Exceptions\VoucherExpired;
9
use BeyondCode\Vouchers\Exceptions\VoucherIsInvalid;
10
use BeyondCode\Vouchers\Exceptions\VoucherAlreadyRedeemed;
11
12
trait CanRedeemVouchers
13
{
14
    /**
15
     * @param string $code
16
     * @throws VoucherExpired
17
     * @throws VoucherIsInvalid
18
     * @throws VoucherAlreadyRedeemed
19
     * @return mixed
20
     */
21
    public function redeemCode(string $code)
22
    {
23
        $voucher = Vouchers::check($code);
24
25
        if ($voucher->users()->wherePivot('user_id', $this->id)->exists()) {
0 ignored issues
show
Bug introduced by
The property id 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...
26
            throw VoucherAlreadyRedeemed::create($voucher);
27
        }
28
        if ($voucher->isExpired()) {
29
            throw VoucherExpired::create($voucher);
30
        }
31
32
        $this->vouchers()->attach($voucher, [
33
            'redeemed_at' => now()
34
        ]);
35
36
        event(new VoucherRedeemed($this, $voucher));
37
38
        return $voucher;
39
    }
40
41
    /**
42
     * @param Voucher $voucher
43
     * @throws VoucherExpired
44
     * @throws VoucherIsInvalid
45
     * @throws VoucherAlreadyRedeemed
46
     * @return mixed
47
     */
48
    public function redeemVoucher(Voucher $voucher)
49
    {
50
        return $this->redeemCode($voucher->code);
51
    }
52
53
    /**
54
     * @return mixed
55
     */
56
    public function vouchers()
57
    {
58
        return $this->belongsToMany(Voucher::class)->withPivot('redeemed_at');
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
59
    }
60
}
61