-
Notifications
You must be signed in to change notification settings - Fork 94
Improvements #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improvements #72
Changes from all commits
725e74e
614fb01
a5d4115
54bb02d
19c93a0
8bb8059
3eafe29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Relaticle\ImportWizard\Data; | ||
|
|
||
| use Illuminate\Support\Facades\Cache; | ||
| use Relaticle\ImportWizard\Enums\PreviewStatus; | ||
| use Spatie\LaravelData\Attributes\MapName; | ||
| use Spatie\LaravelData\Data; | ||
| use Spatie\LaravelData\Mappers\SnakeCaseMapper; | ||
|
|
||
| #[MapName(SnakeCaseMapper::class)] | ||
| final class ImportSessionData extends Data | ||
| { | ||
| private const int DEFAULT_TTL_HOURS = 24; | ||
|
|
||
| public function __construct( | ||
| public string $teamId, | ||
| public string $inputHash, | ||
| public int $total, | ||
| public int $processed = 0, | ||
| public int $creates = 0, | ||
| public int $updates = 0, | ||
| public int $newCompanies = 0, | ||
| public ?int $heartbeat = null, | ||
| public ?string $error = null, | ||
| ) {} | ||
|
|
||
| public static function find(string $sessionId): ?self | ||
| { | ||
| $data = Cache::get(self::cacheKey($sessionId)); | ||
|
|
||
| return $data !== null ? self::from($data) : null; | ||
| } | ||
|
|
||
| public function status(): PreviewStatus | ||
| { | ||
| return match (true) { | ||
| $this->error !== null => PreviewStatus::Failed, | ||
| $this->processed >= $this->total => PreviewStatus::Ready, | ||
| default => PreviewStatus::Processing, | ||
| }; | ||
| } | ||
|
|
||
| public function isHeartbeatStale(int $thresholdSeconds = 30): bool | ||
| { | ||
| return $this->heartbeat !== null | ||
| && (int) now()->timestamp - $this->heartbeat > $thresholdSeconds; | ||
| } | ||
|
|
||
| public function refresh(string $sessionId): void | ||
| { | ||
| Cache::put( | ||
| self::cacheKey($sessionId), | ||
| [...$this->toArray(), 'heartbeat' => (int) now()->timestamp], | ||
| now()->addHours(self::ttlHours()) | ||
| ); | ||
| } | ||
|
|
||
| /** @param array<string, mixed> $changes */ | ||
| public static function update(string $sessionId, array $changes): void | ||
| { | ||
| $data = self::find($sessionId); | ||
|
|
||
| if (! $data instanceof self) { | ||
| return; | ||
| } | ||
|
|
||
| Cache::put( | ||
| self::cacheKey($sessionId), | ||
| [...$data->toArray(), ...$changes], | ||
| now()->addHours(self::ttlHours()) | ||
| ); | ||
| } | ||
|
Comment on lines
+62
to
+75
|
||
|
|
||
| public function save(string $sessionId): void | ||
| { | ||
| Cache::put(self::cacheKey($sessionId), $this->toArray(), now()->addHours(self::ttlHours())); | ||
| } | ||
|
|
||
| public static function forget(string $sessionId): void | ||
| { | ||
| Cache::forget(self::cacheKey($sessionId)); | ||
| } | ||
|
|
||
| public static function cacheKey(string $sessionId): string | ||
| { | ||
| return "import:{$sessionId}"; | ||
| } | ||
|
|
||
| public static function ttlHours(): int | ||
| { | ||
| return (int) config('import-wizard.session_ttl_hours', self::DEFAULT_TTL_HOURS); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
refresh()method updates the cache with a spread of$this->toArray()plus the new heartbeat, but it doesn't update the current object's$this->heartbeatproperty. This could lead to inconsistency if the same ImportSessionData instance is used after callingrefresh(). Consider updating the object's heartbeat property as well, or making it clear through naming/documentation that this method only updates the cache.