codes.txt
This commit is contained in:
15
app/Exceptions/CouponPoolExhaustedException.php
Normal file
15
app/Exceptions/CouponPoolExhaustedException.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class CouponPoolExhaustedException extends RuntimeException
|
||||
{
|
||||
public const MESSAGE = 'Aksiýa tamamlandy. Gyzyklanmagyňyz üçin sag boluň.';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::MESSAGE);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,11 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\OtpVerificationResult;
|
||||
use App\Exceptions\CouponPoolExhaustedException;
|
||||
use App\Http\Requests\SendOtpRequest;
|
||||
use App\Http\Requests\VerifyOtpRequest;
|
||||
use App\Models\Coupon;
|
||||
use App\Services\CouponCodePool;
|
||||
use App\Services\CouponService;
|
||||
use App\Services\OtpService;
|
||||
use Illuminate\Contracts\View\View;
|
||||
@@ -20,6 +22,7 @@ class VerificationController extends Controller
|
||||
public function __construct(
|
||||
private OtpService $otpService,
|
||||
private CouponService $couponService,
|
||||
private CouponCodePool $couponCodePool,
|
||||
) {}
|
||||
|
||||
public function index(): View|RedirectResponse
|
||||
@@ -28,6 +31,10 @@ class VerificationController extends Controller
|
||||
return $redirect;
|
||||
}
|
||||
|
||||
if (! $this->couponCodePool->hasAvailable()) {
|
||||
return view('verification.promotion-ended');
|
||||
}
|
||||
|
||||
return view('verification.index');
|
||||
}
|
||||
|
||||
@@ -44,6 +51,10 @@ class VerificationController extends Controller
|
||||
return back()->withErrors(['phone' => 'Bu telefon belgisi eýýäm ulanyldy.'])->withInput();
|
||||
}
|
||||
|
||||
if (! $this->couponCodePool->hasAvailable()) {
|
||||
return back()->withErrors(['phone' => CouponPoolExhaustedException::MESSAGE])->withInput();
|
||||
}
|
||||
|
||||
if (! $this->otpService->issue($phone)) {
|
||||
return back()->withErrors(['phone' => 'SMS iberilmedi. Soňrak synanyşyň.'])->withInput();
|
||||
}
|
||||
@@ -63,6 +74,12 @@ class VerificationController extends Controller
|
||||
return redirect()->route('verification.index');
|
||||
}
|
||||
|
||||
$phone = session()->get('verify_phone');
|
||||
|
||||
if (! $this->couponCodePool->hasAvailable() && ! Coupon::query()->where('phone', $phone)->exists()) {
|
||||
return redirect()->route('verification.index');
|
||||
}
|
||||
|
||||
return view('verification.verify', [
|
||||
'phone' => session()->get('verify_phone'),
|
||||
]);
|
||||
@@ -84,6 +101,10 @@ class VerificationController extends Controller
|
||||
return redirect()->route('verification.congratulations');
|
||||
}
|
||||
|
||||
if (! $this->couponCodePool->hasAvailable()) {
|
||||
return redirect()->route('verification.index');
|
||||
}
|
||||
|
||||
if (! $this->otpService->issue($phone)) {
|
||||
return back()->withErrors(['otp' => 'SMS iberilmedi. Soňrak synanyşyň.']);
|
||||
}
|
||||
@@ -109,7 +130,11 @@ class VerificationController extends Controller
|
||||
return back()->withErrors(['otp' => 'Nädogry kod.']);
|
||||
}
|
||||
|
||||
$coupon = $this->couponService->findOrCreateForPhone($phone);
|
||||
try {
|
||||
$coupon = $this->couponService->findOrCreateForPhone($phone);
|
||||
} catch (CouponPoolExhaustedException) {
|
||||
return back()->withErrors(['otp' => CouponPoolExhaustedException::MESSAGE]);
|
||||
}
|
||||
|
||||
session()->put('coupon_code', $coupon->code);
|
||||
|
||||
|
||||
77
app/Services/CouponCodePool.php
Normal file
77
app/Services/CouponCodePool.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Coupon;
|
||||
|
||||
class CouponCodePool
|
||||
{
|
||||
/** @var list<string>|null */
|
||||
private ?array $allCodesCache = null;
|
||||
|
||||
public function __construct(
|
||||
private ?string $codesPath = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public function allCodes(): array
|
||||
{
|
||||
if ($this->allCodesCache !== null) {
|
||||
return $this->allCodesCache;
|
||||
}
|
||||
|
||||
$path = $this->codesPath ?? resource_path('codes/codes.txt');
|
||||
|
||||
if (! is_readable($path)) {
|
||||
throw new \RuntimeException("Coupon codes file is not readable: {$path}");
|
||||
}
|
||||
|
||||
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
|
||||
if ($lines === false) {
|
||||
throw new \RuntimeException("Unable to read coupon codes file: {$path}");
|
||||
}
|
||||
|
||||
$codes = array_values(array_filter(array_map(trim(...), $lines)));
|
||||
|
||||
if ($codes === []) {
|
||||
throw new \RuntimeException("Coupon codes file is empty: {$path}");
|
||||
}
|
||||
|
||||
return $this->allCodesCache = $codes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public function usedCodes(): array
|
||||
{
|
||||
return Coupon::query()->pluck('code')->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public function availableCodes(): array
|
||||
{
|
||||
return array_values(array_diff($this->allCodes(), $this->usedCodes()));
|
||||
}
|
||||
|
||||
public function hasAvailable(): bool
|
||||
{
|
||||
return $this->availableCodes() !== [];
|
||||
}
|
||||
|
||||
public function pickRandom(): ?string
|
||||
{
|
||||
$available = $this->availableCodes();
|
||||
|
||||
if ($available === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $available[array_rand($available)];
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,16 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\CouponPoolExhaustedException;
|
||||
use App\Models\Coupon;
|
||||
use Illuminate\Database\UniqueConstraintViolationException;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CouponService
|
||||
{
|
||||
public function __construct(
|
||||
private CouponCodePool $couponCodePool,
|
||||
) {}
|
||||
|
||||
public function findOrCreateForPhone(string $phone): Coupon
|
||||
{
|
||||
$existing = Coupon::query()->where('phone', $phone)->first();
|
||||
@@ -22,24 +26,22 @@ class CouponService
|
||||
private function createWithUniqueCode(string $phone): Coupon
|
||||
{
|
||||
for ($attempt = 0; $attempt < 10; $attempt++) {
|
||||
$code = $this->couponCodePool->pickRandom();
|
||||
|
||||
if ($code === null) {
|
||||
throw new CouponPoolExhaustedException;
|
||||
}
|
||||
|
||||
try {
|
||||
return Coupon::query()->create([
|
||||
'phone' => $phone,
|
||||
'code' => $this->generateCouponCode(),
|
||||
'code' => $code,
|
||||
]);
|
||||
} catch (UniqueConstraintViolationException) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \RuntimeException('Unable to generate a unique coupon code.');
|
||||
}
|
||||
|
||||
private function generateCouponCode(): string
|
||||
{
|
||||
$x = random_int(1, 9);
|
||||
$yyyy = Str::upper(Str::random(4));
|
||||
|
||||
return "{$x}_{$yyyy}";
|
||||
throw new CouponPoolExhaustedException;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user