Skip to content

Commit

Permalink
Merge pull request Stampie#12 from Incenteev/rich_identities
Browse files Browse the repository at this point in the history
Added the support for Identity objects to configure the name and email
  • Loading branch information
henrikbjorn committed Dec 13, 2012
2 parents e0eaf8d + 1c043dc commit 48f3d64
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 26 deletions.
41 changes: 41 additions & 0 deletions lib/Stampie/Identity.php
@@ -0,0 +1,41 @@
<?php

namespace Stampie;

/**
* @author Christophe Coevoet <stof@notk.org>
*/
class Identity implements IdentityInterface
{
private $email;
private $name;

public function __construct($email = null)
{
$this->email = $email;
}

public function setEmail($email)
{
$this->email = $email;

return $this;
}

public function getEmail()
{
return $this->email;
}

public function setName($name)
{
$this->name = $name;

return $this;
}

public function getName()
{
return $this->name;
}
}
19 changes: 19 additions & 0 deletions lib/Stampie/IdentityInterface.php
@@ -0,0 +1,19 @@
<?php

namespace Stampie;

/**
* @author Christophe Coevoet <stof@notk.org>
*/
interface IdentityInterface
{
/**
* @return string
*/
public function getEmail();

/**
* @return string|null
*/
public function getName();
}
32 changes: 32 additions & 0 deletions lib/Stampie/Mailer.php
Expand Up @@ -125,4 +125,36 @@ abstract protected function format(MessageInterface $message);
* @throws \Stampie\Exception\HttpException
*/
abstract protected function handle(ResponseInterface $response);

/**
* @param IdentityInterface|string $identity
*
* @return IdentityInterface
*/
protected function normalizeIdentity($identity)
{
if (!$identity instanceof IdentityInterface) {
$identity = new Identity($identity);
}

return $identity;
}

/**
* @param IdentityInterface|string $identity
*
* @return string
*/
protected function buildIdentityString($identity)
{
if ($identity instanceof IdentityInterface) {
if (null === $identity->getName()) {
return $identity->getEmail();
}

return sprintf('%s <%s>', $identity->getName(), $identity->getEmail());
}

return $identity;
}
}
9 changes: 7 additions & 2 deletions lib/Stampie/Mailer/MailChimpSts.php
Expand Up @@ -33,14 +33,19 @@ protected function getEndpoint()
*/
protected function format(MessageInterface $message)
{
$from = $this->normalizeIdentity($message->getFrom());
$to = $this->normalizeIdentity($message->getTo());

$parameters = array(
'apikey' => $this->getServerToken(),
'message' => array_filter(array(
'html' => $message->getHtml(),
'text' => $message->getText(),
'subject' => $message->getSubject(),
'to_email' => $message->getTo(),
'from_email' => $message->getFrom(),
'to_email' => array($to->getEmail()),
'to_name' => array($to->getName()),
'from_email' => $from->getEmail(),
'from_name' => $from->getName(),
)),
);

Expand Down
8 changes: 4 additions & 4 deletions lib/Stampie/Mailer/MailGun.php
Expand Up @@ -58,13 +58,13 @@ protected function format(MessageInterface $message)
});

$parameters = array(
'from' => $message->getFrom(),
'to' => $message->getTo(),
'from' => $this->buildIdentityString($message->getFrom()),
'to' => $this->buildIdentityString($message->getTo()),
'subject' => $message->getSubject(),
'text' => $message->getText(),
'html' => $message->getHtml(),
'cc' => $message->getCc(),
'bcc' => $message->getBcc(),
'cc' => $this->buildIdentityString($message->getCc()),
'bcc' => $this->buildIdentityString($message->getBcc()),
);

return http_build_query(array_filter(array_merge($headers, $parameters)));
Expand Down
9 changes: 7 additions & 2 deletions lib/Stampie/Mailer/Mandrill.php
Expand Up @@ -42,11 +42,16 @@ protected function format(MessageInterface $message)
$message->getHeaders(),
array('Reply-To' => $message->getReplyTo())
));

$from = $this->normalizeIdentity($message->getFrom());
$to = $this->normalizeIdentity($message->getTo());

$parameters = array(
'key' => $this->getServerToken(),
'message' => array_filter(array(
'from_email' => $message->getFrom(),
'to' => array(array('email' => $message->getTo())),
'from_email' => $from->getEmail(),
'from_name' => $from->getName(),
'to' => array(array('email' => $to->getEmail(), 'name' => $to->getName())),
'subject' => $message->getSubject(),
'headers' => $headers,
'text' => $message->getText(),
Expand Down
4 changes: 2 additions & 2 deletions lib/Stampie/Mailer/PeytzMail.php
Expand Up @@ -66,9 +66,9 @@ public function send(MessageInterface $message)
protected function format(MessageInterface $message)
{
$parameters = array(
'email' => $message->getTo(),
'email' => $this->normalizeIdentity($message->getTo())->getEmail(),
'subject' => $message->getSubject(),
'from_email' => $message->getFrom(),
'from_email' => $this->normalizeIdentity($message->getFrom())->getEmail(),
'tag' => $message->getTag(),
'content' => array(
'html' => $message->getHtml(),
Expand Down
4 changes: 2 additions & 2 deletions lib/Stampie/Mailer/Postmark.php
Expand Up @@ -57,8 +57,8 @@ protected function getHeaders()
protected function format(MessageInterface $message)
{
$parameters = array_filter(array(
'From' => $message->getFrom(),
'To' => $message->getTo(),
'From' => $this->buildIdentityString($message->getFrom()),
'To' => $this->buildIdentityString($message->getTo()),
'Subject' => $message->getSubject(),
'Headers' => $message->getHeaders(),
'TextBody' => $message->getText(),
Expand Down
11 changes: 8 additions & 3 deletions lib/Stampie/Mailer/SendGrid.php
Expand Up @@ -60,15 +60,20 @@ protected function format(MessageInterface $message)
// We should split up the ServerToken on : to get username and password
list($username, $password) = explode(':', $this->getServerToken());

$from = $this->normalizeIdentity($message->getFrom());
$to = $this->normalizeIdentity($message->getTo());

$parameters = array(
'api_user' => $username,
'api_key' => $password,
'to' => $message->getTo(),
'from' => $message->getFrom(),
'to' => $to->getEmail(),
'toname' => $to->getName(),
'from' => $from->getEmail(),
'fromname' => $from->getName(),
'subject' => $message->getSubject(),
'text' => $message->getText(),
'html' => $message->getHtml(),
'bcc' => $message->getBcc(),
'bcc' => $this->normalizeIdentity($message->getBcc())->getEmail(),
'replyto' => $message->getReplyTo(),
'headers' => json_encode($message->getHeaders()),
);
Expand Down
1 change: 0 additions & 1 deletion lib/Stampie/MailerInterface.php
Expand Up @@ -3,7 +3,6 @@
namespace Stampie;

use Stampie\Adapter\AdapterInterface;
use Stampie\Adapter\ResponseInterface;

/**
* Takes a MailerInterface and sends to to Postmark throgh Buzz
Expand Down
9 changes: 5 additions & 4 deletions lib/Stampie/Message.php
Expand Up @@ -26,11 +26,12 @@ abstract class Message implements MessageInterface
protected $text;

/**
* @param string $to
* @param IdentityInterface|string $to
*/
public function __construct($to)
{
if (!filter_var($to, FILTER_VALIDATE_EMAIL)) {
$email = $to instanceof IdentityInterface ? $to->getEmail() : $to;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('Invalid email');
}

Expand All @@ -55,7 +56,7 @@ public function setHtml($html)

/**
* @param string $text
* @throws \InvalidArgument
* @throws \InvalidArgumentException
*/
public function setText($text)
{
Expand Down Expand Up @@ -83,7 +84,7 @@ public function getText()
}

/**
* @return true
* @return array
*/
public function getHeaders()
{
Expand Down
8 changes: 4 additions & 4 deletions lib/Stampie/MessageInterface.php
Expand Up @@ -11,22 +11,22 @@
interface MessageInterface
{
/**
* @return string
* @return IdentityInterface|string
*/
function getFrom();

/**
* @return string
* @return IdentityInterface|string
*/
function getTo();

/**
* @return string
* @return IdentityInterface|string
*/
function getCc();

/**
* @return $string
* @return IdentityInterface|$string
*/
function getBcc();

Expand Down
3 changes: 2 additions & 1 deletion tests/Stampie/Tests/Mailer/MailChimpStsTest.php
Expand Up @@ -47,7 +47,8 @@ public function testFormat()
'message' => array(
'html' => $html,
'subject' => $subject,
'to_email' => $to,
'to_email' => array($to),
'to_name' => array(null),
'from_email' => $from,
),
)), $this->mailer->format($message));
Expand Down
2 changes: 1 addition & 1 deletion tests/Stampie/Tests/Mailer/MandrillTest.php
Expand Up @@ -46,7 +46,7 @@ public function testFormat()
'key' => self::SERVER_TOKEN,
'message' => array(
'from_email' => $from,
'to' => array(array('email' => $to)),
'to' => array(array('email' => $to, 'name' => null)),
'subject' => $subject,
'html' => $html,
),
Expand Down

0 comments on commit 48f3d64

Please sign in to comment.