|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MichaelRubel\Couponables\Services; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Support\Traits\Macroable; |
|
9
|
|
|
use MichaelRubel\Couponables\Events\CouponDisabled; |
|
10
|
|
|
use MichaelRubel\Couponables\Events\CouponExpired; |
|
11
|
|
|
use MichaelRubel\Couponables\Events\CouponIsOverLimit; |
|
12
|
|
|
use MichaelRubel\Couponables\Events\CouponIsOverQuantity; |
|
13
|
|
|
use MichaelRubel\Couponables\Events\CouponRedeemed; |
|
14
|
|
|
use MichaelRubel\Couponables\Events\CouponVerified; |
|
15
|
|
|
use MichaelRubel\Couponables\Events\NotAllowedToRedeem; |
|
16
|
|
|
use MichaelRubel\Couponables\Exceptions\CouponDisabledException; |
|
17
|
|
|
use MichaelRubel\Couponables\Exceptions\CouponExpiredException; |
|
18
|
|
|
use MichaelRubel\Couponables\Exceptions\InvalidCouponException; |
|
19
|
|
|
use MichaelRubel\Couponables\Exceptions\NotAllowedToRedeemException; |
|
20
|
|
|
use MichaelRubel\Couponables\Exceptions\OverLimitException; |
|
21
|
|
|
use MichaelRubel\Couponables\Exceptions\OverQuantityException; |
|
22
|
|
|
use MichaelRubel\Couponables\Models\Contracts\CouponContract; |
|
23
|
|
|
use MichaelRubel\Couponables\Models\Contracts\CouponPivotContract; |
|
24
|
|
|
use MichaelRubel\Couponables\Services\Contracts\CouponServiceContract; |
|
25
|
|
|
use MichaelRubel\Couponables\Traits\Concerns\GeneratesCoupons; |
|
26
|
|
|
|
|
27
|
|
|
class CouponService implements CouponServiceContract |
|
28
|
|
|
{ |
|
29
|
|
|
use Macroable, GeneratesCoupons; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param CouponContract $model |
|
33
|
|
|
* @param CouponPivotContract $pivot |
|
34
|
|
|
*/ |
|
35
|
60 |
|
public function __construct( |
|
36
|
|
|
public CouponContract $model, |
|
37
|
|
|
public CouponPivotContract $pivot, |
|
38
|
|
|
) { |
|
39
|
60 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the coupon model by the code. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string|null $code |
|
45
|
|
|
* |
|
46
|
|
|
* @return CouponContract|null |
|
47
|
|
|
*/ |
|
48
|
39 |
|
public function getCoupon(?string $code): ?CouponContract |
|
49
|
|
|
{ |
|
50
|
39 |
|
return $this->model->firstWhere($this->model->getCodeColumn(), $code); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Perform the stateless checks on the coupon |
|
55
|
|
|
* model. Redeemer is optional in this case. |
|
56
|
|
|
* |
|
57
|
|
|
* @param CouponContract $coupon |
|
58
|
|
|
* @param Model|null $redeemer |
|
59
|
|
|
* |
|
60
|
|
|
* @return CouponContract |
|
61
|
|
|
* |
|
62
|
|
|
* @throws CouponExpiredException |
|
63
|
|
|
* @throws OverQuantityException |
|
64
|
|
|
* @throws CouponDisabledException |
|
65
|
|
|
*/ |
|
66
|
30 |
|
public function performBasicChecks(CouponContract $coupon, ?Model $redeemer = null): CouponContract |
|
67
|
|
|
{ |
|
68
|
30 |
|
if ($coupon->isDisabled()) { |
|
|
|
|
|
|
69
|
2 |
|
event(new CouponDisabled($coupon, $redeemer)); |
|
70
|
|
|
|
|
71
|
2 |
|
throw new CouponDisabledException; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
28 |
|
if ($coupon->isExpired()) { |
|
|
|
|
|
|
75
|
2 |
|
event(new CouponExpired($coupon, $redeemer)); |
|
76
|
|
|
|
|
77
|
2 |
|
throw new CouponExpiredException; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
26 |
|
if ($coupon->isOverQuantity()) { |
|
|
|
|
|
|
81
|
2 |
|
event(new CouponIsOverQuantity($coupon, $redeemer)); |
|
82
|
|
|
|
|
83
|
2 |
|
throw new OverQuantityException; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
26 |
|
return $coupon; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Perform the "Redeemer" checks on the coupon model. |
|
91
|
|
|
* |
|
92
|
|
|
* @param CouponContract $coupon |
|
93
|
|
|
* @param Model $redeemer |
|
94
|
|
|
* |
|
95
|
|
|
* @return CouponContract |
|
96
|
|
|
* |
|
97
|
|
|
* @throws OverLimitException |
|
98
|
|
|
* @throws NotAllowedToRedeemException |
|
99
|
|
|
*/ |
|
100
|
26 |
|
public function performChecksOnRedeemer(CouponContract $coupon, Model $redeemer): CouponContract |
|
101
|
|
|
{ |
|
102
|
26 |
|
if (! $coupon->isAllowedToRedeemBy($redeemer)) { |
|
|
|
|
|
|
103
|
5 |
|
event(new NotAllowedToRedeem($coupon, $redeemer)); |
|
104
|
|
|
|
|
105
|
5 |
|
throw new NotAllowedToRedeemException; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
21 |
|
if ($coupon->isOverLimit($redeemer)) { |
|
|
|
|
|
|
109
|
3 |
|
event(new CouponIsOverLimit($coupon, $redeemer)); |
|
110
|
|
|
|
|
111
|
3 |
|
throw new OverLimitException; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
21 |
|
return $coupon; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Verify if coupon is valid otherwise throw an exception. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string|null $code |
|
121
|
|
|
* @param Model|null $redeemer |
|
122
|
|
|
* |
|
123
|
|
|
* @return CouponContract |
|
124
|
|
|
* @throws CouponExpiredException |
|
125
|
|
|
* @throws InvalidCouponException |
|
126
|
|
|
* @throws NotAllowedToRedeemException |
|
127
|
|
|
* @throws OverLimitException |
|
128
|
|
|
* @throws OverQuantityException |
|
129
|
|
|
* @throws CouponDisabledException |
|
130
|
|
|
*/ |
|
131
|
38 |
|
public function verifyCoupon(?string $code, ?Model $redeemer = null): CouponContract |
|
132
|
|
|
{ |
|
133
|
38 |
|
$coupon = $this->getCoupon($code) ?? throw new InvalidCouponException; |
|
134
|
|
|
|
|
135
|
30 |
|
$this->performBasicChecks($coupon); |
|
136
|
|
|
|
|
137
|
26 |
|
if ($redeemer) { |
|
138
|
26 |
|
$this->performChecksOnRedeemer($coupon, $redeemer); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
21 |
|
event(new CouponVerified($coupon, $redeemer)); |
|
142
|
|
|
|
|
143
|
21 |
|
return $coupon; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Apply the coupon. |
|
148
|
|
|
* |
|
149
|
|
|
* @param CouponContract $coupon |
|
150
|
|
|
* @param Model $redeemer |
|
151
|
|
|
* @param Model|null $redeemed |
|
152
|
|
|
* |
|
153
|
|
|
* @return CouponContract |
|
154
|
|
|
*/ |
|
155
|
19 |
|
public function applyCoupon(CouponContract $coupon, Model $redeemer, ?Model $redeemed): CouponContract |
|
156
|
|
|
{ |
|
157
|
19 |
|
$redeemer->coupons()->attach($coupon, [ |
|
158
|
19 |
|
$this->pivot->getRedeemedTypeColumn() => $redeemed?->getMorphClass(), |
|
159
|
19 |
|
$this->pivot->getRedeemedIdColumn() => $redeemed?->id, |
|
160
|
19 |
|
$this->pivot->getRedeemedAtColumn() => now(), |
|
161
|
19 |
|
$this->pivot->getCreatedAtColumn() => now(), |
|
162
|
19 |
|
]); |
|
163
|
|
|
|
|
164
|
19 |
|
if (! is_null($coupon->{$this->model->getQuantityColumn()})) { |
|
165
|
2 |
|
$coupon->decrement($this->model->getQuantityColumn()); |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
19 |
|
event(new CouponRedeemed($coupon, $redeemer)); |
|
169
|
|
|
|
|
170
|
19 |
|
return $coupon; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|