Skip to content

Commit 4bae04c

Browse files
committed
Rename VuFind\Mailer\Factory to MailerFactory for consistency.
1 parent b4de458 commit 4bae04c

File tree

4 files changed

+150
-102
lines changed

4 files changed

+150
-102
lines changed

module/VuFind/config/module.config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@
509509
'VuFind\ILS\Logic\TitleHolds' => 'VuFind\ILS\Logic\LogicFactory',
510510
'VuFind\ILS\HoldSettings' => 'VuFind\ILS\HoldSettingsFactory',
511511
'VuFind\Log\Logger' => 'VuFind\Log\LoggerFactory',
512-
'VuFind\Mailer\Mailer' => 'VuFind\Mailer\Factory',
512+
'VuFind\Mailer\Mailer' => 'VuFind\Mailer\MailerFactory',
513513
'VuFind\MetadataVocabulary\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
514514
'VuFind\Navigation\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
515515
'VuFind\Net\IpAddressUtils' => 'Laminas\ServiceManager\Factory\InvokableFactory',
Lines changed: 4 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Factory for instantiating Mailer objects
4+
* Deprecated legacy factory for instantiating Mailer objects
55
*
66
* PHP version 8
77
*
@@ -31,15 +31,8 @@
3131

3232
namespace VuFind\Mailer;
3333

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-
4134
/**
42-
* Factory for instantiating Mailer objects
35+
* Deprecated legacy factory for instantiating Mailer objects
4336
*
4437
* @category VuFind
4538
* @package Mailer
@@ -48,97 +41,8 @@
4841
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
4942
* @link https://vufind.org/wiki/development Wiki
5043
*
51-
* @codeCoverageIgnore
44+
* @deprecated Use MailerFactory
5245
*/
53-
class Factory implements FactoryInterface
46+
class Factory extends MailerFactory
5447
{
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-
}
14448
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
}

module/VuFind/tests/unit-tests/src/VuFindTest/Mailer/MailerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
use Symfony\Component\Mailer\MailerInterface;
3333
use Symfony\Component\Mime\Address;
34-
use VuFind\Mailer\Factory as MailerFactory;
34+
use VuFind\Mailer\MailerFactory;
3535
use VuFind\Mailer\Mailer;
3636
use VuFindTest\Container\MockContainer;
3737

0 commit comments

Comments
 (0)