52 lines
1.3 KiB
PHP
52 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Observers;
|
|
|
|
use App\Models\UserFlight;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class FlightObserver
|
|
{
|
|
protected function clearCache(UserFlight $flight): void
|
|
{
|
|
Cache::forget("user_flights_{$flight->user->id}");
|
|
|
|
//Make queued task if the site gets big
|
|
$flight->user->followers()
|
|
->get()
|
|
->each(fn ($follower) => Cache::forget("user_following_flights_{$follower->user_id}"));
|
|
}
|
|
|
|
|
|
/**
|
|
* Recalculate after a flight is created.
|
|
*/
|
|
public function created(UserFlight $flight): void
|
|
{
|
|
$flight->user->calculateAchievements();
|
|
$this->clearCache($flight);
|
|
}
|
|
|
|
/**
|
|
* Recalculate after a flight is updated.
|
|
* Cabin class, flight type, airline, etc. may have changed,
|
|
* which could unlock or revoke achievements.
|
|
*/
|
|
public function updated(UserFlight $flight): void
|
|
{
|
|
\Log::info('Observer fired for flight ' . $flight->id);
|
|
$flight->user->calculateAchievements();
|
|
$this->clearCache($flight);
|
|
}
|
|
|
|
/**
|
|
* Recalculate after a flight is deleted.
|
|
* Previously earned achievements may no longer be valid.
|
|
*/
|
|
public function deleted(UserFlight $flight): void
|
|
{
|
|
$flight->user->calculateAchievements();
|
|
$this->clearCache($flight);
|
|
}
|
|
}
|