Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | n/a |
0 / 0 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
|||
Factory | n/a |
0 / 0 |
n/a |
0 / 0 |
13 | n/a |
0 / 0 |
|||
getTransport | n/a |
0 / 0 |
n/a |
0 / 0 |
10 | |||||
__invoke | n/a |
0 / 0 |
n/a |
0 / 0 |
3 |
1 | <?php |
2 | |
3 | /** |
4 | * Factory for instantiating Mailer objects |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2009. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Mailer |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development Wiki |
28 | */ |
29 | |
30 | namespace VuFind\Mailer; |
31 | |
32 | use Laminas\Mail\Transport\InMemory; |
33 | use Laminas\Mail\Transport\Smtp; |
34 | use Laminas\Mail\Transport\SmtpOptions; |
35 | use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
36 | use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
37 | use Laminas\ServiceManager\Factory\FactoryInterface; |
38 | use Psr\Container\ContainerExceptionInterface as ContainerException; |
39 | use Psr\Container\ContainerInterface; |
40 | |
41 | /** |
42 | * Factory for instantiating Mailer objects |
43 | * |
44 | * @category VuFind |
45 | * @package Mailer |
46 | * @author Demian Katz <demian.katz@villanova.edu> |
47 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
48 | * @link https://vufind.org/wiki/development Wiki |
49 | * |
50 | * @codeCoverageIgnore |
51 | */ |
52 | class Factory implements FactoryInterface |
53 | { |
54 | /** |
55 | * Build the mail transport object. |
56 | * |
57 | * @param \Laminas\Config\Config $config Configuration |
58 | * |
59 | * @return InMemory|Smtp |
60 | */ |
61 | protected function getTransport($config) |
62 | { |
63 | // In test mode? Return fake object: |
64 | if (isset($config->Mail->testOnly) && $config->Mail->testOnly) { |
65 | return new InMemory(); |
66 | } |
67 | |
68 | // Create mail transport: |
69 | $settings = [ |
70 | 'host' => $config->Mail->host, 'port' => $config->Mail->port, |
71 | ]; |
72 | if (isset($config->Mail->name)) { |
73 | $settings['name'] = $config->Mail->name; |
74 | } |
75 | if (isset($config->Mail->username) && isset($config->Mail->password)) { |
76 | $settings['connection_class'] = 'login'; |
77 | $settings['connection_config'] = [ |
78 | 'username' => $config->Mail->username, |
79 | 'password' => $config->Mail->password, |
80 | ]; |
81 | // Set user defined secure connection if provided; otherwise set default |
82 | // secure connection based on configured port number. |
83 | if (isset($config->Mail->secure)) { |
84 | $settings['connection_config']['ssl'] = $config->Mail->secure; |
85 | } elseif ($settings['port'] == '587') { |
86 | $settings['connection_config']['ssl'] = 'tls'; |
87 | } elseif ($settings['port'] == '487') { |
88 | $settings['connection_config']['ssl'] = 'ssl'; |
89 | } |
90 | } |
91 | if (isset($config->Mail->connection_time_limit)) { |
92 | $settings['connection_time_limit'] |
93 | = $config->Mail->connection_time_limit; |
94 | } |
95 | return new Smtp(new SmtpOptions($settings)); |
96 | } |
97 | |
98 | /** |
99 | * Create an object |
100 | * |
101 | * @param ContainerInterface $container Service manager |
102 | * @param string $requestedName Service being created |
103 | * @param null|array $options Extra options (optional) |
104 | * |
105 | * @return object |
106 | * |
107 | * @throws ServiceNotFoundException if unable to resolve the service. |
108 | * @throws ServiceNotCreatedException if an exception is raised when |
109 | * creating a service. |
110 | * @throws ContainerException&\Throwable if any other error occurs |
111 | */ |
112 | public function __invoke( |
113 | ContainerInterface $container, |
114 | $requestedName, |
115 | array $options = null |
116 | ) { |
117 | if (!empty($options)) { |
118 | throw new \Exception('Unexpected options passed to factory.'); |
119 | } |
120 | |
121 | // Load configurations: |
122 | $config = $container->get(\VuFind\Config\PluginManager::class) |
123 | ->get('config'); |
124 | |
125 | // Create service: |
126 | $class = new $requestedName( |
127 | $this->getTransport($config), |
128 | $config->Mail->message_log |
129 | ); |
130 | if (!empty($config->Mail->override_from)) { |
131 | $class->setFromAddressOverride($config->Mail->override_from); |
132 | } |
133 | return $class; |
134 | } |
135 | } |