*/ private const AT_MOST = [ 'fewo.guest_retention_years' => 'guest_retention_years', 'fewo.retention.inquiry_months' => 'retention_inquiry_months', 'fewo.retention.portal_email_excerpt_days' => 'retention_portal_email_excerpt_days', 'fewo.retention.attribution_months' => 'retention_attribution_months', 'fewo.spam.per_ip_per_hour' => 'spam_per_ip_per_hour', 'fewo.spam.per_email_per_day' => 'spam_per_email_per_day', 'fewo.spam.open_requests_per_email' => 'spam_open_requests_per_email', 'fewo.spam.max_links' => 'spam_max_links', ]; /** * The time trap works the other way round: a longer minimum time is stricter. * * @var array */ private const AT_LEAST = [ 'fewo.spam.min_seconds' => 'spam_min_seconds', ]; /** * Values the landlord chooses freely; empty means the baseline. * * @var array */ private const FREE = [ 'fewo.deposit_due_days' => 'deposit_due_days', 'fewo.review_request_after_days' => 'review_request_after_days', 'fewo.channels.stale_after_minutes' => 'channel_stale_after_minutes', 'fewo.channels.task_reminder_after_minutes' => 'channel_task_reminder_after_minutes', ]; /** * Data of the landlord: an empty value stays empty instead of falling back to another landlord's. * * @var array */ private const OWNED = [ 'fewo.owner.name' => 'owner_name', 'fewo.owner.street' => 'owner_street', 'fewo.owner.city' => 'owner_city', 'fewo.owner.phone' => 'owner_phone', 'fewo.owner.email' => 'contact_email', 'fewo.bank.account_holder' => 'bank_account_holder', 'fewo.bank.iban' => 'bank_iban', 'fewo.bank.bic' => 'bank_bic', 'fewo.bank.bank_name' => 'bank_name', ]; /** @var array|null */ private ?array $baseline = null; /** * What this class wrote into the config last time. A key that now holds something else was changed from * outside (a test, a command) – that value becomes the new baseline instead of being overwritten again. * * @var array */ private array $written = []; /** * The landlord of the current context, else the only landlord there is (console, boot), else none. */ public function effectiveTenant(): ?Tenant { $current = app(TenantContext::class)->get(); if ($current !== null) { return $current; } try { $tenants = Tenant::query()->limit(2)->get(); } catch (Throwable) { return null; // before the migrations ran } return $tenants->count() === 1 ? $tenants->first() : null; } /** * Applies the effective landlord, or the baseline when there is none. */ public function refresh(): void { $this->apply($this->effectiveTenant()); } public function apply(?Tenant $tenant): void { $baseline = $this->baseline(); $values = $baseline; if ($tenant !== null && ! array_key_exists('owner_name', $tenant->getAttributes())) { $tenant = null; // the migration that adds the columns has not run yet; the baseline stands } if ($tenant !== null) { foreach (self::OWNED as $key => $column) { $values[$key] = $tenant->{$column}; } foreach (self::FREE as $key => $column) { $values[$key] = $tenant->{$column} ?? $baseline[$key]; } foreach (self::AT_MOST as $key => $column) { $values[$key] = min($tenant->{$column} ?? $baseline[$key], $baseline[$key]); } foreach (self::AT_LEAST as $key => $column) { $values[$key] = max($tenant->{$column} ?? $baseline[$key], $baseline[$key]); } $values['mail.from.address'] = $tenant->sender_email ?: $baseline['mail.from.address']; $values['mail.from.name'] = $tenant->sender_name ?: $baseline['mail.from.name']; } config($values); $this->written = $values; $this->applyMailRouting($tenant); } /** * The mailer sends everything to one address while the landlord is in test mode. */ public function applyMailRouting(?Tenant $tenant): void { $manager = Mail::getFacadeRoot(); if (! $manager instanceof MailManager) { return; // Mail::fake() in tests } $manager->purge(); $redirect = Settings::mailRedirectFor($tenant); if (filled($redirect)) { $manager->alwaysTo($redirect); } } /** * The value of a config key as the installation configured it, before any landlord was laid over it. */ public function baselineValue(string $key): mixed { return $this->baseline()[$key] ?? config($key); } /** * The highest value the landlord may enter for a column, or null when it is free. */ public function ceilingFor(string $column): ?int { $key = array_search($column, self::AT_MOST, true); return $key === false ? null : (int) $this->baseline()[$key]; } /** * The lowest value the landlord may enter for a column, or null when it is free. */ public function floorFor(string $column): ?int { $key = array_search($column, self::AT_LEAST, true); return $key === false ? null : (int) $this->baseline()[$key]; } /** * @return array */ private function baseline(): array { $keys = [...array_keys(self::OWNED), ...array_keys(self::FREE), ...array_keys(self::AT_MOST), ...array_keys(self::AT_LEAST), 'mail.from.address', 'mail.from.name']; if ($this->baseline === null) { $this->baseline = array_combine($keys, array_map(fn (string $key): mixed => config($key), $keys)); return $this->baseline; } foreach ($keys as $key) { if (array_key_exists($key, $this->written) && config($key) !== $this->written[$key]) { $this->baseline[$key] = config($key); } } return $this->baseline; } }