Skip to content

Commit

Permalink
Federations code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
JanOppolzer committed Nov 25, 2024
1 parent 452ea37 commit c74102a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 23 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/FederationApprovalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));

}

/**
Expand All @@ -34,6 +35,7 @@ public function destroy(Federation $federation)

$name = $federation->name;
NotificationService::sendModelNotification($federation, new FederationRejected($name));

$federation->forceDelete();

return redirect('federations')
Expand Down
15 changes: 5 additions & 10 deletions app/Jobs/DeleteFederation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -42,7 +39,6 @@ public function getFolderName(): string
*/
public function handle(): void
{

try {
$pathToDirectory = FederationService::getFederationFolderByXmlId($this->getFolderName());
} catch (\Exception $e) {
Expand All @@ -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 {
Expand All @@ -66,6 +62,5 @@ public function handle(): void
Log::warning("Lock not owned by current process or lock lost for key: $lockKey");
}
}

}
}
3 changes: 2 additions & 1 deletion app/Notifications/FederationApproved.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion app/Notifications/FederationRejected.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions app/Services/FederationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class FederationService
{
public static function createFederationFolder(Federation $federation): void
{

Storage::disk(config('storageCfg.name'))->makeDirectory($federation->xml_id);
}

Expand Down Expand Up @@ -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.");
}
}
}
11 changes: 3 additions & 8 deletions app/Services/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -83,6 +79,5 @@ public static function sendUpdateNotification(Entity $entity): void
if (! self::sendRsNotification($entity) && ! self::sendHfDNotification($entity)) {
self::sendModelNotification($entity, new EntityUpdated($entity));
}

}
}

0 comments on commit c74102a

Please sign in to comment.