Skip to content

Commit

Permalink
character filters: trigger data update event with character instead o…
Browse files Browse the repository at this point in the history
…f user
  • Loading branch information
recursivetree committed Aug 20, 2024
1 parent 569f429 commit 0895a49
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 45 deletions.
7 changes: 4 additions & 3 deletions src/Events/CharacterFilterDataUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Seat\Web\Events;

use Illuminate\Queue\SerializesModels;
use Seat\Eveapi\Models\Character\CharacterInfo;
use Seat\Web\Models\User;

/**
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Listeners/CharacterFilterDataUpdatedSquads.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CharacterFilterDataUpdatedSquads
{
public static function handle(CharacterFilterDataUpdate $event)
{
$user = $event->user;
$user = $event->character->user;

$member_squads = $user->squads;

Expand Down
11 changes: 6 additions & 5 deletions src/Observers/AbstractCharacterFilterObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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));
}
}
15 changes: 7 additions & 8 deletions src/Observers/CharacterAffiliationObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}
}
15 changes: 7 additions & 8 deletions src/Observers/CharacterAssetObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}
}
15 changes: 7 additions & 8 deletions src/Observers/CharacterRoleObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions src/Observers/CharacterSkillObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
15 changes: 7 additions & 8 deletions src/Observers/CharacterTitleObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
11 changes: 7 additions & 4 deletions src/Observers/RefreshTokenObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 0895a49

Please sign in to comment.