dev-think-one /
laravel-temporal-key
| 1 | <?php |
||
| 2 | |||
| 3 | namespace TemporalKey\Manager; |
||
| 4 | |||
| 5 | use Carbon\Carbon; |
||
| 6 | use Illuminate\Database\Eloquent\Model; |
||
| 7 | use Illuminate\Support\Str; |
||
| 8 | |||
| 9 | class TmpKey implements TemporalKey |
||
| 10 | { |
||
| 11 | protected string $key; |
||
| 12 | protected ?array $meta = null; |
||
| 13 | |||
| 14 | public static string $type = 'default'; |
||
| 15 | public static int $defaultValidSeconds = 60; |
||
| 16 | public static int $defaultUsageMax = 0; |
||
| 17 | |||
| 18 | 10 | protected function __construct(string $key, ?array $meta = null) |
|
| 19 | { |
||
| 20 | 10 | $this->key = $key; |
|
| 21 | 10 | $this->meta = $meta; |
|
| 22 | } |
||
| 23 | |||
| 24 | 4 | protected static function findModel(string $key): ?Model |
|
| 25 | { |
||
| 26 | 4 | $temporalKey = \TemporalKey\TemporalKey::$storeModel::query() |
|
| 27 | 4 | ->where('key', $key) |
|
| 28 | 4 | ->where('type', static::$type) |
|
| 29 | 4 | ->first(); |
|
| 30 | |||
| 31 | 4 | if (!$temporalKey) { |
|
| 32 | 1 | return null; |
|
| 33 | } |
||
| 34 | |||
| 35 | if ( |
||
| 36 | 4 | !$temporalKey->valid_until?->gt(Carbon::now()) |
|
| 37 | || ( |
||
| 38 | 4 | $temporalKey->usage_max |
|
| 39 | 4 | && $temporalKey->usage_max <= $temporalKey->usage_counter |
|
| 40 | ) |
||
| 41 | ) { |
||
| 42 | 4 | $temporalKey->delete(); |
|
| 43 | |||
| 44 | 4 | return null; |
|
| 45 | } |
||
| 46 | |||
| 47 | 4 | return $temporalKey; |
|
| 48 | } |
||
| 49 | |||
| 50 | |||
| 51 | 1 | public static function findWithoutIncrement(string $key): ?static |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 52 | { |
||
| 53 | 1 | $temporalKey = static::findModel($key); |
|
| 54 | 1 | if ($temporalKey) { |
|
| 55 | 1 | return new static($temporalKey->key, $temporalKey->meta); |
|
| 56 | } |
||
| 57 | |||
| 58 | 1 | return null; |
|
| 59 | } |
||
| 60 | |||
| 61 | |||
| 62 | 4 | public static function find(string $key): ?static |
|
| 63 | { |
||
| 64 | 4 | $temporalKey = static::findModel($key); |
|
| 65 | 4 | if ($temporalKey) { |
|
| 66 | 4 | $temporalKey->increment('usage_counter'); |
|
| 67 | |||
| 68 | 4 | return new static($temporalKey->key, $temporalKey->meta); |
|
| 69 | } |
||
| 70 | |||
| 71 | 4 | return null; |
|
| 72 | } |
||
| 73 | |||
| 74 | 10 | public static function create(?array $meta = null, ?Carbon $validUntil = null, ?int $usageMax = null): static |
|
| 75 | { |
||
| 76 | 10 | $temporalKey = \TemporalKey\TemporalKey::$storeModel::create([ |
|
| 77 | 10 | 'key' => static::generateKey(), |
|
| 78 | 10 | 'type' => static::$type, |
|
| 79 | 10 | 'valid_until' => $validUntil ?? Carbon::now()->addSeconds(static::defaultValidSeconds()), |
|
| 80 | 10 | 'usage_max' => $usageMax ?? static::defaultUsageMax(), |
|
| 81 | 10 | 'meta' => $meta, |
|
| 82 | 10 | ]); |
|
| 83 | |||
| 84 | 10 | return new static($temporalKey->key, $temporalKey->meta); |
|
| 85 | } |
||
| 86 | |||
| 87 | 9 | protected static function defaultValidSeconds(): int |
|
| 88 | { |
||
| 89 | 9 | return static::$defaultValidSeconds; |
|
| 90 | } |
||
| 91 | |||
| 92 | 8 | protected static function defaultUsageMax(): int |
|
| 93 | { |
||
| 94 | 8 | return static::$defaultUsageMax; |
|
| 95 | } |
||
| 96 | |||
| 97 | 10 | protected static function generateKey(): string |
|
| 98 | { |
||
| 99 | 10 | return Str::uuid()->toString(); |
|
| 100 | } |
||
| 101 | |||
| 102 | 9 | public function key(): string |
|
| 103 | { |
||
| 104 | 9 | return $this->key; |
|
| 105 | } |
||
| 106 | |||
| 107 | 8 | public function meta(): ?array |
|
| 108 | { |
||
| 109 | 8 | return $this->meta; |
|
| 110 | } |
||
| 111 | } |
||
| 112 |