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)); |
|
|
|
|
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 |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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 theid
property of an instance of theAccount
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.