forked from beyondcode/laravel-vouchers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVouchers.php
More file actions
executable file
·124 lines (105 loc) · 2.9 KB
/
Copy pathVouchers.php
File metadata and controls
executable file
·124 lines (105 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace BeyondCode\Vouchers;
use BeyondCode\Vouchers\Exceptions\VoucherExpired;
use BeyondCode\Vouchers\Exceptions\VoucherIsInvalid;
use BeyondCode\Vouchers\Models\Voucher;
use Illuminate\Database\Eloquent\Model;
class Vouchers
{
/** @var VoucherGenerator */
private $generator;
/** @var \BeyondCode\Vouchers\Models\Voucher */
private $voucherModel;
public function __construct(VoucherGenerator $generator)
{
$this->generator = $generator;
$this->voucherModel = app(config('vouchers.model', Voucher::class));
}
/**
* Generate the specified amount of codes and return
* an array with all the generated codes.
*
* @param int $amount
* @return array
*/
public function generate(int $amount = 1): array
{
$codes = [];
for ($i = 1; $i <= $amount; $i++) {
$codes[] = $this->getUniqueVoucher();
}
return $codes;
}
/**
* @param Model $model
* @param int $amount
* @param array $data
* @param null $expires_at
* @return array
*/
public function create(Model $model, int $amount = 1, array $data = [], $expires_at = null)
{
$vouchers = [];
foreach ($this->generate($amount) as $voucherCode) {
$vouchers[] = $this->voucherModel->create([
'model_id' => $model->getKey(),
'model_type' => $model->getMorphClass(),
'code' => $voucherCode,
'data' => $data,
'expires_at' => $expires_at,
]);
}
return $vouchers;
}
/**
* @param string $code
* @throws VoucherIsInvalid
* @throws VoucherExpired
* @return Voucher
*/
public function check(string $code)
{
$voucher = $this->voucherModel->whereCode($code)->first();
if (is_null($voucher)) {
throw VoucherIsInvalid::withCode($code);
}
if ($voucher->isExpired()) {
throw VoucherExpired::create($voucher);
}
return $voucher;
}
/**
* @param string $code
* @return bool
*/
public function isValidCode(string $code): bool
{
try {
$this->check($code);
} catch (VoucherIsInvalid $exception) {
return false;
} catch (VoucherExpired $exception) {
return false;
}
return true;
}
/**
* @param Voucher $voucher
* @return bool
*/
public function isValidVoucher(Voucher $voucher): bool
{
return $this->isValidCode($voucher->code);
}
/**
* @return string
*/
protected function getUniqueVoucher(): string
{
$voucher = $this->generator->generateUnique();
while ($this->voucherModel->whereCode($voucher)->count() > 0) {
$voucher = $this->generator->generateUnique();
}
return $voucher;
}
}