From 0895a4957bbf42f1f5b9dd0bc79809818ab58908 Mon Sep 17 00:00:00 2001 From: recursivetree <60423027+recursivetree@users.noreply.github.com> Date: Tue, 20 Aug 2024 11:08:46 +0200 Subject: [PATCH] character filters: trigger data update event with character instead of user --- src/Events/CharacterFilterDataUpdate.php | 7 ++++--- .../CharacterFilterDataUpdatedSquads.php | 2 +- src/Observers/AbstractCharacterFilterObserver.php | 11 ++++++----- src/Observers/CharacterAffiliationObserver.php | 15 +++++++-------- src/Observers/CharacterAssetObserver.php | 15 +++++++-------- src/Observers/CharacterRoleObserver.php | 15 +++++++-------- src/Observers/CharacterSkillObserver.php | 12 ++++++++++++ src/Observers/CharacterTitleObserver.php | 15 +++++++-------- src/Observers/RefreshTokenObserver.php | 11 +++++++---- 9 files changed, 58 insertions(+), 45 deletions(-) diff --git a/src/Events/CharacterFilterDataUpdate.php b/src/Events/CharacterFilterDataUpdate.php index ffb84c707..eadacb7b8 100644 --- a/src/Events/CharacterFilterDataUpdate.php +++ b/src/Events/CharacterFilterDataUpdate.php @@ -3,6 +3,7 @@ namespace Seat\Web\Events; use Illuminate\Queue\SerializesModels; +use Seat\Eveapi\Models\Character\CharacterInfo; use Seat\Web\Models\User; /** @@ -12,13 +13,13 @@ class CharacterFilterDataUpdate { use SerializesModels; - public User $user; + public CharacterInfo $character; /** * @param User $user */ - public function __construct(User $user) + public function __construct(CharacterInfo $character) { - $this->user = $user; + $this->character = $character; } } \ No newline at end of file diff --git a/src/Listeners/CharacterFilterDataUpdatedSquads.php b/src/Listeners/CharacterFilterDataUpdatedSquads.php index 596e1bbd8..55d41e8c2 100644 --- a/src/Listeners/CharacterFilterDataUpdatedSquads.php +++ b/src/Listeners/CharacterFilterDataUpdatedSquads.php @@ -9,7 +9,7 @@ class CharacterFilterDataUpdatedSquads { public static function handle(CharacterFilterDataUpdate $event) { - $user = $event->user; + $user = $event->character->user; $member_squads = $user->squads; diff --git a/src/Observers/AbstractCharacterFilterObserver.php b/src/Observers/AbstractCharacterFilterObserver.php index f46e22db0..1435e4cae 100644 --- a/src/Observers/AbstractCharacterFilterObserver.php +++ b/src/Observers/AbstractCharacterFilterObserver.php @@ -23,6 +23,7 @@ namespace Seat\Web\Observers; use Illuminate\Database\Eloquent\Model; +use Seat\Eveapi\Models\Character\CharacterInfo; use Seat\Web\Events\CharacterFilterDataUpdate; use Seat\Web\Exceptions\InvalidFilterException; use Seat\Web\Models\Squads\Squad; @@ -39,9 +40,9 @@ abstract class AbstractCharacterFilterObserver * Return the User owning the model which fired the catch event. * * @param \Illuminate\Database\Eloquent\Model $fired_model The model which fired the catch event - * @return \Seat\Web\Models\User|null The user owning this model + * @return ?CharacterInfo The character that is affected by this update */ - abstract protected function findRelatedUser(Model $fired_model): ?User; + abstract protected function findRelatedCharacter(Model $fired_model): ?CharacterInfo; /** * Update squads to which the user owning model firing the event is member. @@ -52,11 +53,11 @@ abstract protected function findRelatedUser(Model $fired_model): ?User; */ protected function fireCharacterFilterEvent(Model $fired_model): void { - $user = $this->findRelatedUser($fired_model); + $character = $this->findRelatedCharacter($fired_model); - if (! $user) + if (! $character) return; - event(new CharacterFilterDataUpdate($user)); + event(new CharacterFilterDataUpdate($character)); } } diff --git a/src/Observers/CharacterAffiliationObserver.php b/src/Observers/CharacterAffiliationObserver.php index 420cc1e05..86297fcce 100644 --- a/src/Observers/CharacterAffiliationObserver.php +++ b/src/Observers/CharacterAffiliationObserver.php @@ -24,6 +24,7 @@ use Illuminate\Database\Eloquent\Model; use Seat\Eveapi\Models\Character\CharacterAffiliation; +use Seat\Eveapi\Models\Character\CharacterInfo; use Seat\Web\Models\User; /** @@ -50,15 +51,13 @@ public function updated(CharacterAffiliation $affiliation) } /** - * {@inheritdoc} + * Return the User owning the model which fired the catch event. + * + * @param \Illuminate\Database\Eloquent\Model $fired_model The model which fired the catch event + * @return ?CharacterInfo The character that is affected by this update */ - protected function findRelatedUser(Model $fired_model): ?User + protected function findRelatedCharacter(Model $fired_model): ?CharacterInfo { - // retrieve user related to the character affiliation - return User::with('squads') - ->standard() - ->whereHas('characters', function ($query) use ($fired_model) { - $query->where('character_infos.character_id', $fired_model->character_id); - })->first(); + return $fired_model->character; } } diff --git a/src/Observers/CharacterAssetObserver.php b/src/Observers/CharacterAssetObserver.php index 816d291f1..1e5629bdf 100644 --- a/src/Observers/CharacterAssetObserver.php +++ b/src/Observers/CharacterAssetObserver.php @@ -24,6 +24,7 @@ use Illuminate\Database\Eloquent\Model; use Seat\Eveapi\Models\Assets\CharacterAsset; +use Seat\Eveapi\Models\Character\CharacterInfo; use Seat\Web\Models\User; /** @@ -58,15 +59,13 @@ public function deleted(CharacterAsset $asset) } /** - * {@inheritdoc} + * Return the User owning the model which fired the catch event. + * + * @param \Illuminate\Database\Eloquent\Model $fired_model The model which fired the catch event + * @return ?CharacterInfo The character that is affected by this update */ - protected function findRelatedUser(Model $fired_model): ?User + protected function findRelatedCharacter(Model $fired_model): ?CharacterInfo { - // retrieve user related to the character affiliation - return User::with('squads') - ->standard() - ->whereHas('characters', function ($query) use ($fired_model) { - $query->where('character_infos.character_id', $fired_model->character_id); - })->first(); + return $fired_model->character; } } diff --git a/src/Observers/CharacterRoleObserver.php b/src/Observers/CharacterRoleObserver.php index c9d4813ad..7b64f2c9f 100644 --- a/src/Observers/CharacterRoleObserver.php +++ b/src/Observers/CharacterRoleObserver.php @@ -25,6 +25,7 @@ use Exception; use Illuminate\Database\Eloquent\Model; use Seat\Eveapi\Bus\Corporation; +use Seat\Eveapi\Models\Character\CharacterInfo; use Seat\Eveapi\Models\Character\CharacterRole; use Seat\Eveapi\Models\RefreshToken; use Seat\Web\Models\User; @@ -79,15 +80,13 @@ public function deleted(CharacterRole $role) } /** - * {@inheritdoc} + * Return the User owning the model which fired the catch event. + * + * @param \Illuminate\Database\Eloquent\Model $fired_model The model which fired the catch event + * @return ?CharacterInfo The character that is affected by this update */ - protected function findRelatedUser(Model $fired_model): ?User + protected function findRelatedCharacter(Model $fired_model): ?CharacterInfo { - // retrieve user related to the character affiliation - return User::with('squads') - ->standard() - ->whereHas('characters', function ($query) use ($fired_model) { - $query->where('character_infos.character_id', $fired_model->character_id); - })->first(); + return $fired_model->character; } } diff --git a/src/Observers/CharacterSkillObserver.php b/src/Observers/CharacterSkillObserver.php index f3cf18dd8..2ac2ad3a4 100644 --- a/src/Observers/CharacterSkillObserver.php +++ b/src/Observers/CharacterSkillObserver.php @@ -23,6 +23,7 @@ namespace Seat\Web\Observers; use Illuminate\Database\Eloquent\Model; +use Seat\Eveapi\Models\Character\CharacterInfo; use Seat\Eveapi\Models\Character\CharacterSkill; use Seat\Web\Models\User; @@ -69,4 +70,15 @@ protected function findRelatedUser(Model $fired_model): ?User $query->where('character_infos.character_id', $fired_model->character_id); })->first(); } + + /** + * Return the User owning the model which fired the catch event. + * + * @param \Illuminate\Database\Eloquent\Model $fired_model The model which fired the catch event + * @return ?CharacterInfo The character that is affected by this update + */ + protected function findRelatedCharacter(Model $fired_model): ?CharacterInfo + { + return $fired_model->character; + } } diff --git a/src/Observers/CharacterTitleObserver.php b/src/Observers/CharacterTitleObserver.php index 722cc7999..e3a07fb74 100644 --- a/src/Observers/CharacterTitleObserver.php +++ b/src/Observers/CharacterTitleObserver.php @@ -23,6 +23,7 @@ namespace Seat\Web\Observers; use Illuminate\Database\Eloquent\Model; +use Seat\Eveapi\Models\Character\CharacterInfo; use Seat\Eveapi\Pivot\Character\CharacterTitle; use Seat\Web\Models\User; @@ -58,15 +59,13 @@ public function deleted(CharacterTitle $title) } /** - * {@inheritdoc} + * Return the User owning the model which fired the catch event. + * + * @param \Illuminate\Database\Eloquent\Model $fired_model The model which fired the catch event + * @return ?CharacterInfo The character that is affected by this update */ - protected function findRelatedUser(Model $fired_model): ?User + protected function findRelatedCharacter(Model $fired_model): ?CharacterInfo { - // retrieve user related to the character affiliation - return User::with('squads') - ->standard() - ->whereHas('characters', function ($query) use ($fired_model) { - $query->where('character_infos.character_id', $fired_model->character_id); - })->first(); + return CharacterInfo::find($fired_model->character_id); } } diff --git a/src/Observers/RefreshTokenObserver.php b/src/Observers/RefreshTokenObserver.php index 0c183278c..a32b1aa14 100644 --- a/src/Observers/RefreshTokenObserver.php +++ b/src/Observers/RefreshTokenObserver.php @@ -25,6 +25,7 @@ use Exception; use Illuminate\Database\Eloquent\Model; use Seat\Eveapi\Bus\Character; +use Seat\Eveapi\Models\Character\CharacterInfo; use Seat\Eveapi\Models\RefreshToken; use Seat\Web\Models\User; @@ -100,11 +101,13 @@ public function restored(RefreshToken $token) } /** - * {@inheritdoc} + * Return the User owning the model which fired the catch event. + * + * @param \Illuminate\Database\Eloquent\Model $fired_model The model which fired the catch event + * @return ?CharacterInfo The character that is affected by this update */ - protected function findRelatedUser(Model $fired_model): ?User + protected function findRelatedCharacter(Model $fired_model): ?CharacterInfo { - return User::with('squads') - ->find($fired_model->user_id); + return $fired_model->character; } }