Last active
August 29, 2015 13:57
-
-
Save AmyStephen/9431688 to your computer and use it in GitHub Desktop.
What pattern does this use?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Email implements EmailInterface | |
{ | |
/** | |
* Email Handler | |
* | |
* @var object | |
* @since 1.0 | |
*/ | |
protected $handler; | |
/** | |
* Constructor | |
* | |
* @param EmailInterface $email | |
* | |
* @since 1.0 | |
*/ | |
public function __construct(EmailInterface $email) | |
{ | |
$this->handler = $email; | |
} | |
/** | |
* Return parameter value or default | |
* | |
* @param string $key | |
* @param string $default | |
* | |
* @return mixed | |
* @since 1.0 | |
*/ | |
public function get($key, $default = null) | |
{ | |
return $this->handler->get($key, $default); | |
} | |
/** | |
* Set parameter value | |
* | |
* @param string $key | |
* @param mixed $value | |
* | |
* @return $this | |
* @since 1.0 | |
*/ | |
public function set($key, $value = null) | |
{ | |
$this->handler->set($key, $value); | |
return $this; | |
} | |
/** | |
* Send Email | |
* | |
* @return mixed | |
* @since 1.0 | |
*/ | |
public function send() | |
{ | |
$this->handler->send(); | |
return $this; | |
} | |
} | |
class PhpMailer extends AbstractHandler implements EmailInterface | |
{ | |
// get, set, send methods that interact with phpMailer | |
} | |
class Swiftmailer extends AbstractHandler implements EmailInterface | |
{ | |
// get, set, send methods that interact with Swiftmailer | |
} | |
// To use | |
// 1. Instantiate an Email Handler. | |
$options = array(); | |
$options['mailer_transport'] = 'mail'; | |
$options['site_name'] = 'Sitename'; | |
$class = 'Molajo\\Email\\Swiftmailer'; | |
$handler = new $class($options); | |
// 2. Instantiate the Email class, injecting it with the Handler. | |
$class = 'Molajo\\Email'; | |
$email = new $class($handler); | |
// 3. Set email parameters | |
$email->set('to', '[email protected],Fname Lname'); | |
$email->set('from', '[email protected],Fname Lname'); | |
$email->set('reply_to', '[email protected],FName LName'); | |
$email->set('cc', '[email protected],FName LName'); | |
$email->set('bcc', '[email protected],FName LName'); | |
$email->set('subject', 'Welcome to our Site'); | |
$email->set('body', 'Stuff goes here'); | |
$email->set('mailer_html_or_text', 'text'); | |
// 4. Send Email. | |
$email->send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Combination proxy (the email class proxies to the underlying handler instance) and adapter (specific handlers).