Operating mode in the admin (test mode sends all mail to a test recipient until switched live), bot protection for booking requests, mail rendering tests, mail:test command
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fdcb68d578
commit
d018c97be4
22 files changed
+698
-23
No files matched your search
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Mail\MailManager;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Operational settings from the admin (System › Betrieb), cached. The most important one: the operating mode.
|
||||
* In **test mode** (the default until the site is switched live) every e-mail goes to the test recipient only,
|
||||
* whatever address was entered on the website.
|
||||
*/
|
||||
class Settings
|
||||
{
|
||||
public const MODE = 'operations.mode';
|
||||
|
||||
public const TEST_RECIPIENT = 'operations.test_recipient';
|
||||
|
||||
public const MODE_TEST = 'test';
|
||||
|
||||
public const MODE_LIVE = 'live';
|
||||
|
||||
private const CACHE_KEY = 'settings.all';
|
||||
|
||||
public static function get(string $key, ?string $default = null): ?string
|
||||
{
|
||||
try {
|
||||
$values = Cache::rememberForever(self::CACHE_KEY, fn (): array => Setting::query()->pluck('value', 'key')->all());
|
||||
} catch (Throwable) {
|
||||
return $default; // e.g. before the migrations ran
|
||||
}
|
||||
|
||||
return $values[$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, ?string> $values
|
||||
*/
|
||||
public static function save(array $values): void
|
||||
{
|
||||
foreach ($values as $key => $value) {
|
||||
Setting::query()->updateOrCreate(['key' => $key], ['value' => $value]);
|
||||
}
|
||||
|
||||
Cache::forget(self::CACHE_KEY);
|
||||
self::applyMailRouting();
|
||||
}
|
||||
|
||||
public static function isLive(): bool
|
||||
{
|
||||
return self::get(self::MODE, self::MODE_TEST) === self::MODE_LIVE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Where all e-mails go instead of their real recipients, or null when mails are delivered normally.
|
||||
* FEWO_MAIL_REDIRECT_TO in .env always wins (hard override for test servers).
|
||||
*/
|
||||
public static function mailRedirect(): ?string
|
||||
{
|
||||
$override = config('fewo.mail_redirect_to');
|
||||
if (filled($override)) {
|
||||
return (string) $override;
|
||||
}
|
||||
|
||||
if (self::isLive()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return self::get(self::TEST_RECIPIENT) ?: (config('fewo.notification_email') ?: null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the redirect to the mailer; called at boot and after saving.
|
||||
*/
|
||||
public static function applyMailRouting(): void
|
||||
{
|
||||
$manager = Mail::getFacadeRoot();
|
||||
if (! $manager instanceof MailManager) {
|
||||
return; // Mail::fake() in tests
|
||||
}
|
||||
|
||||
$redirect = self::mailRedirect();
|
||||
$manager->purge();
|
||||
|
||||
if (filled($redirect)) {
|
||||
$manager->alwaysTo($redirect);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user