mirror of
https://github.com/Cybrarist/Discount-Bandit.git
synced 2026-11-24 19:49:54 +01:00
80 lines
1.9 KiB
PHP
80 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Observers;
|
|
|
|
use App\Enums\RoleEnum;
|
|
use App\Helpers\LinkHelper;
|
|
use App\Jobs\CheckIfStoreNeedsToBeEnabledJob;
|
|
use App\Models\Link;
|
|
use Filament\Notifications\Notification;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class LinkObserver
|
|
{
|
|
public function saving(Link $link)
|
|
{
|
|
if (
|
|
Auth::check() &&
|
|
Auth::user()->role == RoleEnum::User->value &&
|
|
Auth::user()->other_settings['max_links'] &&
|
|
Auth::user()->links()->count() >= Auth::user()->other_settings['max_links']
|
|
) {
|
|
|
|
Notification::make()
|
|
->title('You have reached the maximum number of links')
|
|
->body('Please delete some links before adding a new one, or speak to admin')
|
|
->danger()
|
|
->send();
|
|
|
|
throw ValidationException::withMessages([
|
|
'max_links' => 'You have reached the maximum number of links. Please delete some before adding a new one.',
|
|
]);
|
|
|
|
}
|
|
|
|
[$link_base, $link_params] = LinkHelper::prepare_base_key_and_params($link);
|
|
$link->key = $link_base."?".$link_params;
|
|
}
|
|
|
|
/**
|
|
* Handle the Link "created" event.
|
|
*/
|
|
public function created(Link $link): void
|
|
{
|
|
CheckIfStoreNeedsToBeEnabledJob::dispatch($link->store_id);
|
|
}
|
|
|
|
/**
|
|
* Handle the Link "updated" event.
|
|
*/
|
|
public function updated(Link $link): void
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Handle the Link "deleted" event.
|
|
*/
|
|
public function deleted(Link $link): void
|
|
{
|
|
CheckIfStoreNeedsToBeEnabledJob::dispatch($link->store_id);
|
|
}
|
|
|
|
/**
|
|
* Handle the Link "restored" event.
|
|
*/
|
|
public function restored(Link $link): void
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Handle the Link "force deleted" event.
|
|
*/
|
|
public function forceDeleted(Link $link): void
|
|
{
|
|
//
|
|
}
|
|
}
|