diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 7b20143325d6be18c3404e0fb26ee377a8136bb6..dbc0b60624b9b1956537ef2ff2ea26a81fa643fb 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -172,6 +172,7 @@ $config = array( $logger->setConfig($sm->get('VuFind\Config')->get('config')); return $logger; }, + 'VuFind\Mailer' => 'VuFind\Mailer\Factory', 'VuFind\RecordRouter' => function ($sm) { return new \VuFind\Record\Router($sm->get('VuFind\RecordLoader')); }, @@ -225,7 +226,6 @@ $config = array( ), 'invokables' => array( 'VuFind\CacheManager' => 'VuFind\Cache\Manager', - 'VuFind\Mailer' => 'VuFind\Mailer', 'VuFind\RecordLoader' => 'VuFind\Record\Loader', 'VuFind\SessionManager' => 'Zend\Session\SessionManager', 'VuFind\WorldCatUtils' => 'VuFind\Connection\WorldCatUtils', diff --git a/module/VuFind/src/VuFind/Mailer/Factory.php b/module/VuFind/src/VuFind/Mailer/Factory.php new file mode 100644 index 0000000000000000000000000000000000000000..369efe95ad7057c670e04d324620b75e9a119086 --- /dev/null +++ b/module/VuFind/src/VuFind/Mailer/Factory.php @@ -0,0 +1,72 @@ +<?php +/** + * Factory for instantiating Mailer objects + * + * PHP version 5 + * + * Copyright (C) Villanova University 2009. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Mailer + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/vufind2:developer_manual Wiki + */ +namespace VuFind\Mailer; +use Zend\Mail\Transport\Smtp, Zend\Mail\Transport\SmtpOptions, + Zend\ServiceManager\ServiceLocatorInterface; + +/** + * Factory for instantiating Mailer objects + * + * @category VuFind2 + * @package Mailer + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/vufind2:developer_manual Wiki + */ +class Factory implements \Zend\ServiceManager\FactoryInterface +{ + /** + * Create service + * + * @param ServiceLocatorInterface $sm Service manager + * + * @return mixed + */ + public function createService(ServiceLocatorInterface $sm) + { + // Load configurations: + $config = $sm->get('VuFind\Config')->get('config'); + + // Create mail transport: + $settings = array ( + 'host' => $config->Mail->host, 'port' => $config->Mail->port + ); + if (isset($config->Mail->username) && isset($config->Mail->password)) { + $settings['connection_class'] = 'login'; + $settings['connection_config'] = array( + 'username' => $config->Mail->username, + 'password' => $config->Mail->password + ); + } + $transport = new Smtp(); + $transport->setOptions(new SmtpOptions($settings)); + + // Create service: + return new \VuFind\Mailer\Mailer($transport); + } +} diff --git a/module/VuFind/src/VuFind/Mailer.php b/module/VuFind/src/VuFind/Mailer/Mailer.php similarity index 81% rename from module/VuFind/src/VuFind/Mailer.php rename to module/VuFind/src/VuFind/Mailer/Mailer.php index b98943a71f889dfe913136a74c1bc410452f636a..681c8ebdc3a15f4c14196698edb446b672ce8709 100644 --- a/module/VuFind/src/VuFind/Mailer.php +++ b/module/VuFind/src/VuFind/Mailer/Mailer.php @@ -25,9 +25,8 @@ * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org/wiki/vufind2:developer_manual Wiki */ -namespace VuFind; -use VuFind\Config\Reader as ConfigReader, VuFind\Exception\Mail as MailException, - Zend\Mail\Message, Zend\Mail\Transport\Smtp, Zend\Mail\Transport\SmtpOptions; +namespace VuFind\Mailer; +use VuFind\Exception\Mail as MailException, Zend\Mail\Message; /** * VuFind Mailer Class @@ -40,13 +39,6 @@ use VuFind\Config\Reader as ConfigReader, VuFind\Exception\Mail as MailException */ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface { - /** - * Configuration - * - * @var \Zend\Config\Config - */ - protected $config; - /** * Mail transport * @@ -65,16 +57,10 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface * Constructor * * @param \Zend\Mail\Transport\TransportInterface $transport Mail transport - * object (we'll build our own if none is provided). - * @param \Zend\Config\Config $config VuFind configuration - * object (we'll auto-load if none is provided). */ - public function __construct($transport = null, $config = null) + public function __construct(\Zend\Mail\Transport\TransportInterface $transport) { - if (!is_null($transport)) { - $this->setTransport($transport); - } - $this->config = is_null($config) ? ConfigReader::getConfig() : $config; + $this->setTransport($transport); } /** @@ -110,24 +96,6 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface */ public function getTransport() { - // Create transport if it does not already exist: - if (is_null($this->transport)) { - $settings = array ( - 'host' => $this->config->Mail->host, - 'port' => $this->config->Mail->port - ); - if (isset($this->config->Mail->username) - && isset($this->config->Mail->password) - ) { - $settings['connection_class'] = 'login'; - $settings['connection_config'] = array( - 'username' => $this->config->Mail->username, - 'password' => $this->config->Mail->password - ); - } - $this->transport = new Smtp(); - $this->transport->setOptions(new SmtpOptions($settings)); - } return $this->transport; } diff --git a/module/VuFind/src/VuFind/SMS/Mailer.php b/module/VuFind/src/VuFind/SMS/Mailer.php index 22222a978beafaaed2a29fe40c5f69197c60681d..3b5d4aff217cd31dec7ebc62b3eebdd6dfb2cf95 100644 --- a/module/VuFind/src/VuFind/SMS/Mailer.php +++ b/module/VuFind/src/VuFind/SMS/Mailer.php @@ -64,7 +64,7 @@ class Mailer extends AbstractBase /** * VuFind Mailer object * - * @var \VuFind\Mailer + * @var \VuFind\Mailer\Mailer */ protected $mailer; @@ -73,7 +73,7 @@ class Mailer extends AbstractBase * * @param \Zend\Config\Config $config SMS configuration * @param array $options Additional options: defaultFrom (optional) - * and mailer (must be a \VuFind\Mailer object) + * and mailer (must be a \VuFind\Mailer\Mailer object) */ public function __construct(\Zend\Config\Config $config, $options = array()) { @@ -96,9 +96,11 @@ class Mailer extends AbstractBase // Make sure mailer dependency has been injected: if (!isset($options['mailer']) - || !($options['mailer'] instanceof \VuFind\Mailer) + || !($options['mailer'] instanceof \VuFind\Mailer\Mailer) ) { - throw new \Exception('$options["mailer"] must be a \VuFind\Mailer'); + throw new \Exception( + '$options["mailer"] must be a \VuFind\Mailer\Mailer' + ); } $this->mailer = $options['mailer']; }