42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Mail;
|
|
|
|
use App\Models\Mailbox;
|
|
use App\Support\Settings;
|
|
use InvalidArgumentException;
|
|
|
|
/**
|
|
* Which mailbox a given kind of mail goes out from.
|
|
*
|
|
* Read on every resolve rather than cached in a property: a queue worker lives
|
|
* for hours, and a mapping cached at construction would keep sending from the
|
|
* address that was configured when the worker started.
|
|
*/
|
|
final class MailboxResolver
|
|
{
|
|
public function for(string $purpose): ?Mailbox
|
|
{
|
|
if (! in_array($purpose, MailPurpose::ALL, true)) {
|
|
throw new InvalidArgumentException("Unknown mail purpose [{$purpose}].");
|
|
}
|
|
|
|
$box = $this->named(Settings::get(MailPurpose::settingKey($purpose)));
|
|
|
|
if ($box !== null) {
|
|
return $box;
|
|
}
|
|
|
|
// Fall back rather than fail: a mail that goes out from the wrong
|
|
// address is recoverable, one that is never sent may not be noticed.
|
|
return $purpose === MailPurpose::SYSTEM
|
|
? null
|
|
: $this->named(Settings::get(MailPurpose::settingKey(MailPurpose::SYSTEM)));
|
|
}
|
|
|
|
private function named(mixed $key): ?Mailbox
|
|
{
|
|
return is_string($key) && $key !== '' ? Mailbox::findByKey($key) : null;
|
|
}
|
|
}
|