Files
myep-team/src/Config/EmailTextCatalog.php
T
2026-08-17 10:39:19 +02:00

76 lines
2.0 KiB
PHP

<?php
namespace App\Config;
use Symfony\Component\Yaml\Yaml;
/**
* Reads the delivered wording of the admin-editable mails from config/email_texts.yaml.
*
* That wording is the fallback used whenever no email_text row exists for a key, so the
* mails keep working on a fresh database and "reset to default" needs nothing but deleting
* the row.
*
* The file is parsed lazily and memoised: a request that sends no mail and opens no admin
* screen never touches it, and one that does reads it once. Nothing here is validated at
* compile time, so a missing entry surfaces as an error the first time the catalogue is
* built - EmailTextCatalogTest builds all of them.
*/
class EmailTextCatalog
{
/**
* @var array<string, EmailTextDefinition>|null
*/
private ?array $definitions = null;
public function __construct(
private readonly string $emailTextsFile,
) {
}
/**
* @return array<string, EmailTextDefinition> keyed by EmailTextKey value
*/
public function all(): array
{
return $this->definitions ??= $this->build();
}
public function get(EmailTextKey $key): EmailTextDefinition
{
return $this->all()[$key->value];
}
/**
* @return array<string, EmailTextDefinition>
*/
private function build(): array
{
$config = Yaml::parseFile($this->emailTextsFile);
$descriptions = $config['placeholders'];
$definitions = [];
foreach (EmailTextKey::cases() as $key) {
$text = $config['texts'][$key->value];
$placeholders = [];
foreach ($text['placeholders'] as $name) {
$placeholders[$name] = $descriptions[$name];
}
$definitions[$key->value] = new EmailTextDefinition(
$key,
$text['label'],
$placeholders,
$text['subject'],
$text['headline'],
$text['body'],
);
}
return $definitions;
}
}