Files
2026-10-23 07:14:25 +02:00

143 lines
4.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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';
/** Comma-separated: who is informed about new requests, answered offers, portal events … */
public const NOTIFICATION_RECIPIENTS = 'addresses.notifications';
/** Contact address shown on the website and in the contract; reply-to of guest mails. */
public const CONTACT_EMAIL = 'addresses.contact';
public const SENDER_EMAIL = 'addresses.sender_email';
public const SENDER_NAME = 'addresses.sender_name';
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::applyAddressOverrides();
self::applyMailRouting();
}
/**
* The recipients of internal notifications (admin setting, else FEWO_NOTIFICATION_EMAIL).
*
* @return list<string>
*/
public static function notificationRecipients(): array
{
return self::addressList(self::get(self::NOTIFICATION_RECIPIENTS) ?: (string) config('fewo.notification_email'));
}
/**
* "a@x.de, b@y.de; c@z.de" → valid addresses only.
*
* @return list<string>
*/
public static function addressList(?string $value): array
{
return array_values(array_filter(
array_map('trim', preg_split('/[,;\s]+/', (string) $value) ?: []),
fn (string $address): bool => filter_var($address, FILTER_VALIDATE_EMAIL) !== false,
));
}
/**
* Admin settings for the contact address and the sender override the .env values for this request.
*/
public static function applyAddressOverrides(): void
{
$overrides = array_filter([
'fewo.owner.email' => self::get(self::CONTACT_EMAIL),
'mail.from.address' => self::get(self::SENDER_EMAIL),
'mail.from.name' => self::get(self::SENDER_NAME),
]);
if ($overrides !== []) {
config($overrides);
}
}
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) ?: (self::notificationRecipients()[0] ?? 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);
}
}
}