Passed
Pull Request — master (#51)
by
unknown
11:58
created

Vouchers   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
eloc 36
c 4
b 0
f 2
dl 0
loc 113
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isValidVoucher() 0 3 1
A check() 0 12 3
A isValidCode() 0 11 3
A getUniqueVoucher() 0 9 2
A create() 0 15 2
A generate() 0 9 2
1
<?php
2
3
namespace BeyondCode\Vouchers;
4
5
use BeyondCode\Vouchers\Exceptions\VoucherExpired;
6
use BeyondCode\Vouchers\Exceptions\VoucherIsInvalid;
7
use BeyondCode\Vouchers\Models\Voucher;
8
use Illuminate\Database\Eloquent\Model;
9
10
class Vouchers
11
{
12
    /** @var VoucherGenerator */
13
    private $generator;
14
    /** @var \BeyondCode\Vouchers\Models\Voucher  */
15
    private $voucherModel;
16
17
    public function __construct(VoucherGenerator $generator)
18
    {
19
        $this->generator = $generator;
20
        $this->voucherModel = app(config('vouchers.model', Voucher::class));
0 ignored issues
show
Documentation Bug introduced by
It seems like app(config('vouchers.mod...Models\Voucher::class)) can also be of type Illuminate\Contracts\Foundation\Application. However, the property $voucherModel is declared as type BeyondCode\Vouchers\Models\Voucher. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
21
    }
22
23
    /**
24
     * Generate the specified amount of codes and return
25
     * an array with all the generated codes.
26
     *
27
     * @param int $amount
28
     * @return array
29
     */
30
    public function generate(int $amount = 1): array
31
    {
32
        $codes = [];
33
34
        for ($i = 1; $i <= $amount; $i++) {
35
            $codes[] = $this->getUniqueVoucher();
36
        }
37
38
        return $codes;
39
    }
40
41
    /**
42
     * @param Model $model
43
     * @param int $amount
44
     * @param array $data
45
     * @param null $expires_at
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $expires_at is correct as it would always require null to be passed?
Loading history...
46
     * @return array
47
     */
48
    public function create(Model $model, int $amount = 1, array $data = [], $expires_at = null)
49
    {
50
        $vouchers = [];
51
52
        foreach ($this->generate($amount) as $voucherCode) {
53
            $vouchers[] = $this->voucherModel->create([
54
                'model_id' => $model->getKey(),
55
                'model_type' => $model->getMorphClass(),
56
                'code' => $voucherCode,
57
                'data' => $data,
58
                'expires_at' => $expires_at,
59
            ]);
60
        }
61
62
        return $vouchers;
63
    }
64
65
    /**
66
     * @param string $code
67
     * @throws VoucherIsInvalid
68
     * @throws VoucherExpired
69
     * @return Voucher
70
     */
71
    public function check(string $code)
72
    {
73
        $voucher = $this->voucherModel->whereCode($code)->first();
74
75
        if (is_null($voucher)) {
76
            throw VoucherIsInvalid::withCode($code);
77
        }
78
        if ($voucher->isExpired()) {
79
            throw VoucherExpired::create($voucher);
80
        }
81
82
        return $voucher;
83
    }
84
85
    /**
86
     * @param string $code
87
     * @return bool
88
     */
89
    public function isValidCode(string $code): bool
90
    {
91
        try {
92
            $this->check($code);
93
        } catch (VoucherIsInvalid $exception) {
94
            return false;
95
        } catch (VoucherExpired $exception) {
96
            return false;
97
        }
98
99
        return true;
100
    }
101
102
    /**
103
     * @param Voucher $voucher
104
     * @return bool
105
     */
106
    public function isValidVoucher(Voucher $voucher): bool
107
    {
108
        return $this->isValidCode($voucher->code);
0 ignored issues
show
Bug introduced by
The property code does not seem to exist on BeyondCode\Vouchers\Models\Voucher. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    protected function getUniqueVoucher(): string
115
    {
116
        $voucher = $this->generator->generateUnique();
117
118
        while ($this->voucherModel->whereCode($voucher)->count() > 0) {
119
            $voucher = $this->generator->generateUnique();
120
        }
121
122
        return $voucher;
123
    }
124
}
125