codes.txt
This commit is contained in:
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