From c74102a2632e427139aebb45a453517d741c8b8a Mon Sep 17 00:00:00 2001 From: Jan Oppolzer Date: Mon, 25 Nov 2024 15:34:27 +0100 Subject: [PATCH] Federations code cleaning --- .../Controllers/FederationApprovalController.php | 4 +++- app/Jobs/DeleteFederation.php | 15 +++++---------- app/Notifications/FederationApproved.php | 3 ++- app/Notifications/FederationRejected.php | 3 ++- app/Services/FederationService.php | 3 +-- app/Services/NotificationService.php | 11 +++-------- 6 files changed, 16 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/FederationApprovalController.php b/app/Http/Controllers/FederationApprovalController.php index 24cf4ef..3845e04 100644 --- a/app/Http/Controllers/FederationApprovalController.php +++ b/app/Http/Controllers/FederationApprovalController.php @@ -15,14 +15,15 @@ class FederationApprovalController extends Controller public function store(Federation $federation) { $this->authorize('do-everything'); + $federation->approved = true; $federation->update(); + NotificationService::sendModelNotification($federation, new FederationApproved($federation)); return redirect() ->route('federations.show', $federation) ->with('status', __('federations.approved', ['name' => $federation->name])); - } /** @@ -34,6 +35,7 @@ public function destroy(Federation $federation) $name = $federation->name; NotificationService::sendModelNotification($federation, new FederationRejected($name)); + $federation->forceDelete(); return redirect('federations') diff --git a/app/Jobs/DeleteFederation.php b/app/Jobs/DeleteFederation.php index 3daeae5..c7d2642 100644 --- a/app/Jobs/DeleteFederation.php +++ b/app/Jobs/DeleteFederation.php @@ -22,15 +22,12 @@ class DeleteFederation implements ShouldQueue */ use HandlesJobsFailuresTrait; - private string $folderName; - /** * Create a new job instance. */ - public function __construct(string $folderName) - { - $this->folderName = $folderName; - } + public function __construct( + private string $folderName + ) {} public function getFolderName(): string { @@ -42,7 +39,6 @@ public function getFolderName(): string */ public function handle(): void { - try { $pathToDirectory = FederationService::getFederationFolderByXmlId($this->getFolderName()); } catch (\Exception $e) { @@ -51,12 +47,12 @@ public function handle(): void return; } - $lockKey = 'directory-'.md5($pathToDirectory).'-lock'; + $lockKey = 'directory-' . md5($pathToDirectory) . '-lock'; $lock = Cache::lock($lockKey, config('constants.lock_constant')); + try { $lock->block(config('constants.lock_constant')); FederationService::deleteFederationFolderByXmlId($this->getFolderName()); - } catch (Exception $e) { $this->fail($e); } finally { @@ -66,6 +62,5 @@ public function handle(): void Log::warning("Lock not owned by current process or lock lost for key: $lockKey"); } } - } } diff --git a/app/Notifications/FederationApproved.php b/app/Notifications/FederationApproved.php index a806f27..4e6106d 100644 --- a/app/Notifications/FederationApproved.php +++ b/app/Notifications/FederationApproved.php @@ -4,10 +4,11 @@ use App\Models\Federation; use Illuminate\Bus\Queueable; +use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; -class FederationApproved extends Notification +class FederationApproved extends Notification implements ShouldQueue { use Queueable; diff --git a/app/Notifications/FederationRejected.php b/app/Notifications/FederationRejected.php index 806ed0c..b9db41e 100644 --- a/app/Notifications/FederationRejected.php +++ b/app/Notifications/FederationRejected.php @@ -3,10 +3,11 @@ namespace App\Notifications; use Illuminate\Bus\Queueable; +use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; -class FederationRejected extends Notification +class FederationRejected extends Notification implements ShouldQueue { use Queueable; diff --git a/app/Services/FederationService.php b/app/Services/FederationService.php index a574744..bd8b2ad 100644 --- a/app/Services/FederationService.php +++ b/app/Services/FederationService.php @@ -9,7 +9,6 @@ class FederationService { public static function createFederationFolder(Federation $federation): void { - Storage::disk(config('storageCfg.name'))->makeDirectory($federation->xml_id); } @@ -65,7 +64,7 @@ public static function getFederationFolderByXmlId(string $xmlId): string if ($disk->exists($xmlId)) { return $disk->path($xmlId); } else { - throw new \Exception('Directory does not exist.'); + throw new \Exception("Directory {$xmlId} does not exist."); } } } diff --git a/app/Services/NotificationService.php b/app/Services/NotificationService.php index d4b2ae5..5a956ae 100644 --- a/app/Services/NotificationService.php +++ b/app/Services/NotificationService.php @@ -21,9 +21,7 @@ public static function sendModelNotification(Model $model, $notification): void throw new \InvalidArgumentException('The given model does not have an operators relationship.'); } - $operators = $model->operators; - - self::sendOperatorNotification($operators, $notification); + self::sendOperatorNotification($model->operators, $notification); } public static function sendOperatorNotification(Collection $operators, $notification): void @@ -33,16 +31,14 @@ public static function sendOperatorNotification(Collection $operators, $notifica } $admins = User::activeAdmins()->select('id', 'email')->get(); - $operatorIds = $operators->pluck('id'); $filteredAdmins = $admins->filter(function ($admin) use ($operatorIds) { return ! $operatorIds->contains($admin->id); }); - Notification::sendNow($operators, $notification); - - Notification::sendNow($filteredAdmins, $notification); + Notification::send($operators, $notification); + Notification::send($filteredAdmins, $notification); } private static function sendRsNotification(Entity $entity): bool @@ -83,6 +79,5 @@ public static function sendUpdateNotification(Entity $entity): void if (! self::sendRsNotification($entity) && ! self::sendHfDNotification($entity)) { self::sendModelNotification($entity, new EntityUpdated($entity)); } - } }