Last active
August 29, 2015 14:17
-
-
Save mageekguy/77a1f5a08d22fbd00554 to your computer and use it in GitHub Desktop.
east oriented mailqueue management [WIP]
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 | |
namespace api\data; | |
final class data | |
{ | |
private | |
$value | |
; | |
function __construct($value) | |
{ | |
if (! is_string($value)) | |
{ | |
throw new \domainException('Data should be a string'); | |
} | |
$this->value = $value; | |
} | |
function __toString() | |
{ | |
return $this->value; | |
} | |
} | |
namespace api\mail; | |
final class id | |
{ | |
private | |
$value | |
; | |
function __construct($value) | |
{ | |
if (! is_numeric($value)) | |
{ | |
throw new \domainException('Mail Id should be a string or a number'); | |
} | |
$this->value = (string) $value; | |
} | |
function __toString() | |
{ | |
return $this->value; | |
} | |
} | |
final class address | |
{ | |
private | |
$value | |
; | |
function __construct($value) | |
{ | |
if (! filter_var($value, FILTER_VALIDATE_EMAIL)) | |
{ | |
throw new \domainException('Mail address is invalid'); | |
} | |
$this->value = $value; | |
} | |
function __toString() | |
{ | |
return $this->value; | |
} | |
} | |
final class subject | |
{ | |
private | |
$value | |
; | |
function __construct($value) | |
{ | |
if (! is_string($value)) | |
{ | |
throw new \domainException('Mail subject should be a string'); | |
} | |
$this->value = $value; | |
} | |
function __toString() | |
{ | |
return $this->value; | |
} | |
} | |
final class body | |
{ | |
private | |
$value | |
; | |
function __construct($value) | |
{ | |
if (! is_string($value)) | |
{ | |
throw new \domainException('Mail body should be a string'); | |
} | |
$this->value = $value; | |
} | |
function __toString() | |
{ | |
return $this->value; | |
} | |
} | |
namespace api\mailer; | |
use | |
api\mail | |
; | |
interface controller | |
{ | |
function mailSentHasId(mail\id $mailId); | |
function mailNotSentHasId(mail\id $mailId); | |
} | |
namespace api\mailqueue; | |
use | |
api\mail, | |
api\mailer | |
; | |
interface controller extends mailer\controller | |
{ | |
} | |
namespace api; | |
use | |
api\mail, | |
api\mailer, | |
api\mailqueue | |
; | |
class blackhole | |
{ | |
function __call($method, $arguments) | |
{ | |
return $this; | |
} | |
} | |
class mailer | |
{ | |
private | |
$controller | |
; | |
function __construct(mailer\controller $controller = null) | |
{ | |
$this->controller = $controller ?: new blackhole; | |
} | |
function controllerIs(mailer\controller $controller) | |
{ | |
return new self($controller); | |
} | |
function mailIdAndSenderAndRecipientAndSubjectAndBodyAre(mail\id $id, mail\address $sender, mail\address $recipient, mail\subject $subject, mail\body $body) | |
{ | |
echo 'In mailer, mail with ID \'' . $id . '\' has subject ' . $subject . ' and body ' . $body . PHP_EOL; | |
rand(0, 1) | |
? | |
$this->controller->mailSentHasId($id) | |
: | |
$this->controller->mailNotSentHasId($id) | |
; | |
return $this; | |
} | |
} | |
class backend | |
{ | |
private | |
$mails, | |
$errors | |
; | |
function __construct() | |
{ | |
$this->mails = []; | |
$this->errors = []; | |
} | |
function mailSenderAndRecipientAndSubjectAndBodyAre(mail\address $sender, mail\address $recipient, mail\subject $subject, mail\body $body) | |
{ | |
$this->mails[] = [ new mail\id(sizeof($this->mails) + 1), $sender, $recipient, $subject, $body ]; | |
return $this; | |
} | |
function mailerIs(mailer $mailer) | |
{ | |
$mails = $this->mails; | |
foreach ($mails as $mail) | |
{ | |
$mailer->mailIdAndSenderAndRecipientAndSubjectAndBodyAre($mail[0], $mail[1], $mail[2], $mail[3], $mail[4]); | |
} | |
return $this; | |
} | |
function mailSentHasId(mail\id $mailId) | |
{ | |
if (isset($this->mails[(string) $mailId])) | |
{ | |
unset($this->mails[(string) $mailId]); | |
} | |
echo 'In backend, mail with ID \'' . $mailId . '\' successfully sent' . PHP_EOL; | |
return $this; | |
} | |
function mailNotSentHasId(mail\id $mailId) | |
{ | |
$id = (string) $mailId; | |
if (isset($this->mails[$id])) | |
{ | |
$this->errors[$id] = $this->mails[$id]; | |
unset($this->mails[$id]); | |
} | |
echo 'In backend, mail with ID \'' . $mailId . '\' was not send' . PHP_EOL; | |
return $this; | |
} | |
} | |
class mailqueue implements mailer\controller | |
{ | |
private | |
$backend, | |
$controller | |
; | |
function __construct(backend $backend) | |
{ | |
$this->backend = $backend; | |
$this->controller = new blackhole; | |
} | |
function controllerIs(mailqueue\controller $controller) | |
{ | |
$mailqueue = clone $this; | |
$mailqueue->controller = $controller; | |
return $mailqueue; | |
} | |
function mailSenderAndRecipientAndSubjectAndBodyAre(data\data $sender, data\data $recipient, data\data $subject, data\data $body) | |
{ | |
$mailSender = null; | |
$mailRecipient = null; | |
$mailSubject = null; | |
$mailBody = null; | |
try | |
{ | |
$mailSender = new mail\address((string) $sender); | |
} | |
catch (\domainException $domainException) {} | |
try | |
{ | |
$mailRecipient = new mail\address((string) $recipient); | |
} | |
catch (\domainException $domainException) {} | |
try | |
{ | |
$mailSubject = new mail\subject((string) $subject); | |
} | |
catch (\domainException $domainException) {} | |
try | |
{ | |
$mailBody = new mail\body((string) $body); | |
} | |
catch (\domainException $domainException) {} | |
if ($mailSender && $mailRecipient && $mailSubject && $mailBody) | |
{ | |
$this->backend->mailSenderAndRecipientAndSubjectAndBodyAre($mailSender, $mailRecipient, $mailSubject, $mailBody); | |
} | |
return $this; | |
} | |
function mailerIs(mailer $mailer) | |
{ | |
$this->backend->mailerIs($mailer->controllerIs($this)); | |
return $this; | |
} | |
function mailSentHasId(mail\id $mailId) | |
{ | |
echo 'In mailqueue, mail with ID \'' . $mailId . '\' successfully sent' . PHP_EOL; | |
$this->backend->mailSentHasId($mailId); | |
$this->controller->mailSentHasId($mailId); | |
return $this; | |
} | |
function mailNotSentHasId(mail\id $mailId) | |
{ | |
echo 'In mailqueue, mail with ID \'' . $mailId . '\' was not send' . PHP_EOL; | |
$this->backend->mailNotSentHasId($mailId); | |
$this->controller->mailNotSentHasId($mailId); | |
return $this; | |
} | |
} | |
class controller implements mailqueue\controller | |
{ | |
private | |
$mailqueue | |
; | |
function __construct(mailqueue $mailqueue) | |
{ | |
$this->mailqueue = $mailqueue->controllerIs($this); | |
} | |
function mailSenderAndRecipientAndSubjectAndBodyAre(data\data $sender, data\data $recipient, data\data $subject, data\data $body) | |
{ | |
$this->mailqueue->mailSenderAndRecipientAndSubjectAndBodyAre($sender, $recipient, $subject, $body); | |
return $this; | |
} | |
function mailerIs(mailer $mailer) | |
{ | |
$this->mailqueue->mailerIs($mailer); | |
return $this; | |
} | |
function mailSentHasId(mail\id $mailId) | |
{ | |
echo 'Mail with ID \'' . $mailId . '\' successfully sent' . PHP_EOL; | |
return $this; | |
} | |
function mailNotSentHasId(mail\id $mailId) | |
{ | |
echo 'Mail with ID \'' . $mailId . '\' was not send' . PHP_EOL; | |
return $this; | |
} | |
} | |
(new controller(new mailqueue(new backend))) | |
->mailSenderAndRecipientAndSubjectAndBodyAre(new data\data('frederic.hardy@mageekbox.net'), new data\data('bertrand.malet@advertstream.com'), new data\data('Hello!'), new data\data('World!')) | |
->mailerIs(new mailer) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment