|
13 | 13 |
|
14 | 14 | namespace PhalconKit\Provider\Mailer; |
15 | 15 |
|
16 | | -use PhalconKit\Di\DiInterface; |
17 | 16 | use Phalcon\Events\ManagerInterface; |
18 | 17 | use Phalcon\Incubator\Mailer\Manager; |
| 18 | +use PhalconKit\Di\DiInterface; |
| 19 | +use PhalconKit\Exception\ConfigurationException; |
19 | 20 | use PhalconKit\Provider\AbstractServiceProvider; |
| 21 | +use PHPMailer\PHPMailer\PHPMailer; |
20 | 22 |
|
21 | 23 | /** |
22 | 24 | * Registers the mailer manager service. |
23 | 25 | * |
24 | | - * Mailer configuration is resolved from `mailer.driver`, `mailer.defaults`, |
| 26 | + * Mailer configuration is resolved from `mailer.driver`, `mailer.default`, |
25 | 27 | * and `mailer.drivers.<driver>`. Driver options are merged over defaults before |
26 | 28 | * constructing Phalcon Incubator's mailer manager, then the DI container and |
27 | 29 | * shared events manager are attached when available. |
28 | 30 | */ |
29 | 31 | class ServiceProvider extends AbstractServiceProvider |
30 | 32 | { |
| 33 | + /** |
| 34 | + * @var string[] |
| 35 | + */ |
| 36 | + private const array SUPPORTED_DRIVERS = [ |
| 37 | + 'sendmail', |
| 38 | + 'smtp', |
| 39 | + ]; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var string[] |
| 43 | + */ |
| 44 | + private const array SMTP_ENCRYPTIONS = [ |
| 45 | + '', |
| 46 | + PHPMailer::ENCRYPTION_SMTPS, |
| 47 | + PHPMailer::ENCRYPTION_STARTTLS, |
| 48 | + ]; |
| 49 | + |
31 | 50 | protected string $serviceName = 'mailer'; |
32 | | - |
| 51 | + |
33 | 52 | /** |
34 | 53 | * Register the shared `mailer` service. |
35 | 54 | * |
36 | | - * The SMTP driver enables PHPMailer authentication explicitly because SMTP |
37 | | - * credentials in the merged options imply authenticated transport. |
| 55 | + * SMTP encryption is normalized case-insensitively and validated before the |
| 56 | + * mailer is created so bad config fails before network I/O. The SMTP driver |
| 57 | + * also enables PHPMailer authentication explicitly because SMTP credentials |
| 58 | + * in the merged options imply authenticated transport. |
| 59 | + * |
| 60 | + * @throws ConfigurationException When the selected driver, option shape, or |
| 61 | + * SMTP encryption value is invalid. |
38 | 62 | */ |
39 | 63 | #[\Override] |
40 | 64 | public function register(DiInterface $di): void |
41 | 65 | { |
42 | 66 | $di->setShared($this->getName(), function () use ($di) { |
43 | | - |
44 | 67 | $config = $di->getConfig(); |
45 | | - |
| 68 | + |
46 | 69 | $mailerConfig = $config->pathToArray('mailer', []); |
47 | | - |
48 | | - $driver = $mailerConfig['driver'] ?? ''; |
49 | | - $defaultOptions = $mailerConfig['defaults'] ?? []; |
50 | | - $driverOptions = $mailerConfig['drivers'][$driver] ?? []; |
51 | | - $options = array_merge($defaultOptions, $driverOptions); |
52 | | - |
| 70 | + |
| 71 | + $driver = self::normalizeMailerToken($mailerConfig['driver'] ?? '', 'mailer.driver'); |
| 72 | + self::assertSupportedDriver($driver); |
| 73 | + |
| 74 | + $defaultOptions = self::resolveDefaultOptions($mailerConfig); |
| 75 | + $drivers = self::normalizeDrivers($mailerConfig['drivers'] ?? null); |
| 76 | + $driverOptions = $drivers[$driver] ?? []; |
| 77 | + $options = self::normalizeOptions( |
| 78 | + array_merge($defaultOptions, $driverOptions), |
| 79 | + $driver |
| 80 | + ); |
| 81 | + |
53 | 82 | $manager = new Manager($options); |
54 | 83 | $manager->setDI($di); |
55 | | - |
| 84 | + |
56 | 85 | $eventsManager = $di->get('eventsManager'); |
57 | 86 | if ($eventsManager instanceof ManagerInterface) { |
58 | 87 | $manager->setEventsManager($eventsManager); |
59 | 88 | } |
60 | | - |
| 89 | + |
61 | 90 | if ($driver === 'smtp') { |
62 | 91 | $manager->getMailer()->SMTPAuth = true; |
63 | 92 | } |
64 | | - |
| 93 | + |
65 | 94 | return $manager; |
66 | 95 | }); |
67 | 96 | } |
| 97 | + |
| 98 | + /** |
| 99 | + * Normalize and validate the options passed to the incubator mailer. |
| 100 | + * |
| 101 | + * @param array<mixed> $options |
| 102 | + * |
| 103 | + * @return array<mixed> |
| 104 | + * |
| 105 | + * @throws ConfigurationException When SMTP encryption is unsupported. |
| 106 | + */ |
| 107 | + private static function normalizeOptions(array $options, string $driver): array |
| 108 | + { |
| 109 | + $configuredDriver = self::normalizeMailerToken( |
| 110 | + $options['driver'] ?? $driver, |
| 111 | + 'mailer driver option' |
| 112 | + ); |
| 113 | + if ($configuredDriver !== $driver) { |
| 114 | + throw new ConfigurationException(sprintf( |
| 115 | + 'Mailer driver option "%s" does not match selected driver "%s".', |
| 116 | + $configuredDriver, |
| 117 | + $driver |
| 118 | + )); |
| 119 | + } |
| 120 | + |
| 121 | + $options['driver'] = $driver; |
| 122 | + |
| 123 | + if ($driver !== 'smtp' || !array_key_exists('encryption', $options)) { |
| 124 | + return $options; |
| 125 | + } |
| 126 | + |
| 127 | + $encryption = $options['encryption']; |
| 128 | + if ($encryption === null || $encryption === false) { |
| 129 | + $encryption = ''; |
| 130 | + } elseif (!is_string($encryption)) { |
| 131 | + throw new ConfigurationException( |
| 132 | + 'Mailer SMTP encryption must be a string, false, or null.' |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + $encryption = self::normalizeOptionalMailerToken($encryption); |
| 137 | + if (!in_array($encryption, self::SMTP_ENCRYPTIONS, true)) { |
| 138 | + throw new ConfigurationException(sprintf( |
| 139 | + 'Unsupported mailer SMTP encryption "%s"; use "ssl", "tls", or an empty string.', |
| 140 | + $options['encryption'] |
| 141 | + )); |
| 142 | + } |
| 143 | + |
| 144 | + $options['encryption'] = $encryption; |
| 145 | + |
| 146 | + return $options; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Return validated defaults from the canonical key or legacy alias. |
| 151 | + * |
| 152 | + * @param array<mixed> $mailerConfig |
| 153 | + * |
| 154 | + * @return array<mixed> |
| 155 | + */ |
| 156 | + private static function resolveDefaultOptions(array $mailerConfig): array |
| 157 | + { |
| 158 | + $defaults = []; |
| 159 | + |
| 160 | + if (array_key_exists('defaults', $mailerConfig)) { |
| 161 | + $defaults = self::normalizeOptionArray($mailerConfig['defaults'], 'mailer.defaults'); |
| 162 | + } |
| 163 | + |
| 164 | + if (array_key_exists('default', $mailerConfig)) { |
| 165 | + $defaults = array_merge( |
| 166 | + $defaults, |
| 167 | + self::normalizeOptionArray($mailerConfig['default'], 'mailer.default') |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + return $defaults; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Normalize and validate driver option groups keyed by driver name. |
| 176 | + * |
| 177 | + * @return array<string, array<mixed>> |
| 178 | + */ |
| 179 | + private static function normalizeDrivers(mixed $drivers): array |
| 180 | + { |
| 181 | + if ($drivers === null) { |
| 182 | + return []; |
| 183 | + } |
| 184 | + |
| 185 | + if (!is_array($drivers)) { |
| 186 | + throw new ConfigurationException('Mailer drivers config must be an array.'); |
| 187 | + } |
| 188 | + |
| 189 | + $normalized = []; |
| 190 | + foreach ($drivers as $driver => $options) { |
| 191 | + $driver = self::normalizeMailerToken($driver, 'mailer.drivers key'); |
| 192 | + $normalized[$driver] = self::normalizeOptionArray( |
| 193 | + $options, |
| 194 | + sprintf('mailer.drivers.%s', $driver) |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + return $normalized; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Validate a mailer option group. |
| 203 | + * |
| 204 | + * @return array<mixed> |
| 205 | + */ |
| 206 | + private static function normalizeOptionArray(mixed $options, string $path): array |
| 207 | + { |
| 208 | + if ($options === null) { |
| 209 | + return []; |
| 210 | + } |
| 211 | + |
| 212 | + if (!is_array($options)) { |
| 213 | + throw new ConfigurationException(sprintf('%s must be an array.', $path)); |
| 214 | + } |
| 215 | + |
| 216 | + return $options; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Validate the selected driver against the providers this class can create. |
| 221 | + */ |
| 222 | + private static function assertSupportedDriver(string $driver): void |
| 223 | + { |
| 224 | + if (!in_array($driver, self::SUPPORTED_DRIVERS, true)) { |
| 225 | + throw new ConfigurationException(sprintf( |
| 226 | + 'Unsupported mailer driver "%s"; use "sendmail" or "smtp".', |
| 227 | + $driver |
| 228 | + )); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Normalize ASCII mailer config tokens such as driver and encryption names. |
| 234 | + */ |
| 235 | + private static function normalizeMailerToken(mixed $value, string $path): string |
| 236 | + { |
| 237 | + if (!is_string($value)) { |
| 238 | + throw new ConfigurationException(sprintf('%s must be a string.', $path)); |
| 239 | + } |
| 240 | + |
| 241 | + $value = strtolower(trim($value)); |
| 242 | + if ($value === '') { |
| 243 | + throw new ConfigurationException(sprintf('%s must not be empty.', $path)); |
| 244 | + } |
| 245 | + |
| 246 | + return $value; |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * Normalize optional mailer config tokens that may intentionally be empty. |
| 251 | + */ |
| 252 | + private static function normalizeOptionalMailerToken(string $value): string |
| 253 | + { |
| 254 | + return strtolower(trim($value)); |
| 255 | + } |
68 | 256 | } |
0 commit comments