|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Factory for instantiating Mailer objects |
| 5 | + * |
| 6 | + * PHP version 8 |
| 7 | + * |
| 8 | + * Copyright (C) Villanova University 2009. |
| 9 | + * Copyright (C) The National Library of Finland 2024. |
| 10 | + * |
| 11 | + * This program is free software; you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU General Public License version 2, |
| 13 | + * as published by the Free Software Foundation. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program; if not, see |
| 22 | + * <https://www.gnu.org/licenses/>. |
| 23 | + * |
| 24 | + * @category VuFind |
| 25 | + * @package Mailer |
| 26 | + * @author Demian Katz <demian.katz@villanova.edu> |
| 27 | + * @author Ere Maijala <ere.maijala@helsinki.fi> |
| 28 | + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
| 29 | + * @link https://vufind.org/wiki/development Wiki |
| 30 | + */ |
| 31 | + |
| 32 | +namespace VuFind\Mailer; |
| 33 | + |
| 34 | +use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
| 35 | +use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
| 36 | +use Laminas\ServiceManager\Factory\FactoryInterface; |
| 37 | +use Psr\Container\ContainerExceptionInterface as ContainerException; |
| 38 | +use Psr\Container\ContainerInterface; |
| 39 | +use VuFind\Config\Feature\SecretTrait; |
| 40 | + |
| 41 | +/** |
| 42 | + * Factory for instantiating Mailer objects |
| 43 | + * |
| 44 | + * @category VuFind |
| 45 | + * @package Mailer |
| 46 | + * @author Demian Katz <demian.katz@villanova.edu> |
| 47 | + * @author Ere Maijala <ere.maijala@helsinki.fi> |
| 48 | + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
| 49 | + * @link https://vufind.org/wiki/development Wiki |
| 50 | + * |
| 51 | + * @codeCoverageIgnore |
| 52 | + */ |
| 53 | +class MailerFactory implements FactoryInterface |
| 54 | +{ |
| 55 | + use SecretTrait; |
| 56 | + |
| 57 | + /** |
| 58 | + * Return DSN from the configuration |
| 59 | + * |
| 60 | + * @param array $config Configuration |
| 61 | + * |
| 62 | + * @return string |
| 63 | + */ |
| 64 | + protected function getDSN(array $config): string |
| 65 | + { |
| 66 | + // In test mode? Use null transport: |
| 67 | + if ($config['Mail']['testOnly'] ?? false) { |
| 68 | + return 'null://null'; |
| 69 | + } |
| 70 | + |
| 71 | + if ($dsn = $config['Mail']['dsn'] ?? null) { |
| 72 | + return $dsn; |
| 73 | + } |
| 74 | + |
| 75 | + // Create DSN from settings: |
| 76 | + $protocol = ($config['Mail']['secure'] ?? false) ? 'smtps' : 'smtp'; |
| 77 | + $dsn = "$protocol://"; |
| 78 | + if ( |
| 79 | + ('' !== ($username = rawurlencode($config['Mail']['username'] ?? ''))) |
| 80 | + && ('' !== ($password = rawurlencode($this->getSecretFromConfig($config['Mail'], 'password') ?? ''))) |
| 81 | + ) { |
| 82 | + $dsn .= "$username:$password@"; |
| 83 | + } |
| 84 | + $dsn .= $config['Mail']['host']; |
| 85 | + if ($port = $config['Mail']['port'] ?? null) { |
| 86 | + $dsn .= ":$port"; |
| 87 | + } |
| 88 | + |
| 89 | + $dsnParams = []; |
| 90 | + if ($name = $config['Mail']['name'] ?? null) { |
| 91 | + $dsnParams['local_domain'] = $name; |
| 92 | + } |
| 93 | + if (null !== ($limit = $config['Mail']['connection_time_limit'] ?? null)) { |
| 94 | + $dsnParams['ping_threshold'] = $limit; |
| 95 | + } |
| 96 | + if ($dsnParams) { |
| 97 | + $dsn .= '?' . http_build_query($dsnParams); |
| 98 | + } |
| 99 | + |
| 100 | + return $dsn; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Create an object |
| 105 | + * |
| 106 | + * @param ContainerInterface $container Service manager |
| 107 | + * @param string $requestedName Service being created |
| 108 | + * @param null|array $options Extra options (optional) |
| 109 | + * |
| 110 | + * @return object |
| 111 | + * |
| 112 | + * @throws ServiceNotFoundException if unable to resolve the service. |
| 113 | + * @throws ServiceNotCreatedException if an exception is raised when |
| 114 | + * creating a service. |
| 115 | + * @throws ContainerException&\Throwable if any other error occurs |
| 116 | + */ |
| 117 | + public function __invoke( |
| 118 | + ContainerInterface $container, |
| 119 | + $requestedName, |
| 120 | + ?array $options = null |
| 121 | + ) { |
| 122 | + if (!empty($options)) { |
| 123 | + throw new \Exception('Unexpected options passed to factory.'); |
| 124 | + } |
| 125 | + |
| 126 | + // Load configurations: |
| 127 | + $config = $container->get(\VuFind\Config\ConfigManagerInterface::class)->getConfigArray('config'); |
| 128 | + |
| 129 | + // Create service: |
| 130 | + $class = new $requestedName( |
| 131 | + new \Symfony\Component\Mailer\Mailer( |
| 132 | + \Symfony\Component\Mailer\Transport::fromDsn($this->getDSN($config)) |
| 133 | + ), |
| 134 | + [ |
| 135 | + 'message_log' => $config['Mail']['message_log'] ?? null, |
| 136 | + 'message_log_format' => $config['Mail']['message_log_format'] ?? null, |
| 137 | + ] |
| 138 | + ); |
| 139 | + if ($fromOverride = $config['Mail']['override_from'] ?? null) { |
| 140 | + $class->setFromAddressOverride($fromOverride); |
| 141 | + } |
| 142 | + return $class; |
| 143 | + } |
| 144 | +} |
0 commit comments