Skip to content

Commit

Permalink
Removed callback usage when processing attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
adamaveray authored and Ninir committed Jul 16, 2014
1 parent cf4b132 commit dba21be
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 162 deletions.
13 changes: 0 additions & 13 deletions lib/Stampie/Mailer.php
Expand Up @@ -5,7 +5,6 @@
use Stampie\Adapter\AdapterInterface;
use Stampie\Adapter\ResponseInterface;
use Stampie\Util\IdentityUtils;
use Stampie\Util\AttachmentUtils;

/**
* Minimal implementation of a MailerInterface
Expand Down Expand Up @@ -174,16 +173,4 @@ protected function buildIdentityString($identities)
{
return IdentityUtils::buildIdentityString($identities);
}

/**
* @param AttachmentInterface[] $attachments
* @param callable $callback function (string $name, AttachmentInterface $attachment) { ... }
*
* @return array
* @see \Stampie\Utils\AttachmentUtils::processAttachments
*/
protected function processAttachments(array $attachments, $callback)
{
return AttachmentUtils::processAttachments($attachments, $callback);
}
}
35 changes: 19 additions & 16 deletions lib/Stampie/Mailer/MailGun.php
Expand Up @@ -61,19 +61,7 @@ protected function getFiles(MessageInterface $message)
}

// Process files
$inline = array();
$attachments = $this->processAttachments($message->getAttachments(), function ($name, AttachmentInterface $attachment) use (&$inline) {
$path = $attachment->getPath();
$id = $attachment->getID();
if (isset($id)) {
// Inline
$inline[] = $path;
return null; // Do not add to attachments
}

// Attached
return $path;
});
list($attachments, $inline) = $this->processAttachments($message->getAttachments());

// Format params
$files = array();
Expand Down Expand Up @@ -135,10 +123,25 @@ protected function handle(ResponseInterface $response)
}

/**
* {@inheritdoc}
* @param AttachmentInterface[] $attachments
* @return array First element: An array of attachment paths. Second element: An array of inline paths
*/
protected function processAttachments(array $attachments, $callback)
protected function processAttachments(array $attachments)
{
return array_values(parent::processAttachments($attachments, $callback));
$processedAttachments = array();
$inline = array();
foreach ($attachments as $attachment) {
$path = $attachment->getPath();
$id = $attachment->getID();
if (isset($id)) {
// Inline
$inline[] = $path;
} else {
// Attached
$processedAttachments[] = $path;
}
}

return array($processedAttachments, $inline);
}
}
74 changes: 46 additions & 28 deletions lib/Stampie/Mailer/Mandrill.php
Expand Up @@ -11,6 +11,7 @@
use Stampie\AttachmentInterface;
use Stampie\Exception\HttpException;
use Stampie\Exception\ApiException;
use Stampie\Util\AttachmentUtils;

/**
* Sends emails to Mandrill server
Expand Down Expand Up @@ -72,29 +73,10 @@ protected function format(MessageInterface $message)
$metadata = array_filter($message->getMetadata());
}

$self = $this; // PHP5.3 compatibility
$images = array();
$attachments = array();
if ($message instanceof AttachmentsInterface) {
$attachments = $this->processAttachments($message->getAttachments(), function ($name, AttachmentInterface $attachment) use (&$images, $self) {
$type = $attachment->getType();
$item = array(
'type' => $type,
'name' => $name,
'content' => base64_encode($self->getAttachmentContent($attachment)),
);

$id = $attachment->getID();
if (strpos($type, 'image/') === 0 && isset($id)) {
// Inline image
$item['name'] = $id;
$images[] = $item;

return null; // Do not add to attachments
}

return $item;
});
list($attachments, $images) = $this->processAttachments($message->getAttachments());
}

$parameters = array(
Expand Down Expand Up @@ -131,20 +113,56 @@ protected function handle(ResponseInterface $response)
}

/**
* @param AttachmentInterface $attachment
* @return string
* @param AttachmentInterface[] $attachments
* @return array
* First element: Attachments – an array containing arrays of the following format
* array(
* 'type' => type,
* 'name' => name,
* 'content' => base64-encoded content,
* )
*
* Second element: Inline images – an array containing arrays of the following format
* array(
* 'type' => type,
* 'name' => id,
* 'content' => base64-encoded content,
* )
*/
protected function getAttachmentContent(AttachmentInterface $attachment)
protected function processAttachments(array $attachments)
{
return file_get_contents($attachment->getPath());
$attachments = AttachmentUtils::processAttachments($attachments);

$processedAttachments = array();
$images = array();
foreach ($attachments as $name => $attachment) {
$type = $attachment->getType();
$item = array(
'type' => $type,
'name' => $name,
'content' => base64_encode($this->getAttachmentContent($attachment)),
);

$id = $attachment->getID();
if (strpos($type, 'image/') === 0 && isset($id)) {
// Inline image
$item['name'] = $id;
$images[] = $item;
} else {
// Attached
$processedAttachments[] = $item;
}
}

return array($processedAttachments, $images);
}

/**
* {@inheritdoc}
* @param AttachmentInterface $attachment
* @return string
*/
protected function processAttachments(array $attachments, $callback)
protected function getAttachmentContent(AttachmentInterface $attachment)
{
// Strip keys
return array_values(parent::processAttachments($attachments, $callback));
return file_get_contents($attachment->getPath());
}
}
51 changes: 31 additions & 20 deletions lib/Stampie/Mailer/Postmark.php
Expand Up @@ -10,6 +10,7 @@
use Stampie\AttachmentInterface;
use Stampie\Exception\HttpException;
use Stampie\Exception\ApiException;
use Stampie\Util\AttachmentUtils;

/**
* Sends emails to Postmark server
Expand Down Expand Up @@ -85,24 +86,10 @@ protected function format(MessageInterface $message)
}

if ($message instanceof AttachmentsInterface) {
$self = $this; // PHP5.3 compatibility
$attachments = $this->processAttachments($message->getAttachments(), function ($name, AttachmentInterface $attachment) use ($self) {
$item = array(
'Name' => $name,
'Content' => base64_encode($self->getAttachmentContent($attachment)),
'ContentType' => $attachment->getType(),
);

$id = $attachment->getID();
if (isset($id)) {
$item['ContentID'] = $id;
}

return $item;
});
$attachments = $this->processAttachments($message->getAttachments());

if ($attachments) {
$parameters['Attachments'] = $attachments;
$parameters['Attachments'] = $attachments;
}
}

Expand All @@ -119,11 +106,35 @@ protected function getAttachmentContent(AttachmentInterface $attachment)
}

/**
* {@inheritdoc}
* @param AttachmentInterface[] $attachments
* @return array An array containing arrays of the following format:
* array(
* 'Name' => name,
* 'Content' => base64-encoded content,
* 'ContentType' => type,
* (optional) 'ContentID' => id,
* )
*/
protected function processAttachments(array $attachments, $callback)
protected function processAttachments(array $attachments)
{
// Strip keys
return array_values(parent::processAttachments($attachments, $callback));
$attachments = AttachmentUtils::processAttachments($attachments);

$processedAttachments = array();
foreach ($attachments as $name => $attachment) {
$item = array(
'Name' => $name,
'Content' => base64_encode($this->getAttachmentContent($attachment)),
'ContentType' => $attachment->getType(),
);

$id = $attachment->getID();
if (isset($id)) {
$item['ContentID'] = $id;
}

$processedAttachments[] = $item;
}

return $processedAttachments;
}
}
38 changes: 26 additions & 12 deletions lib/Stampie/Mailer/SendGrid.php
Expand Up @@ -11,6 +11,7 @@
use Stampie\AttachmentInterface;
use Stampie\Exception\HttpException;
use Stampie\Exception\ApiException;
use Stampie\Util\AttachmentUtils;

/**
* Mailer to be used with SendGrid Web API
Expand Down Expand Up @@ -50,9 +51,7 @@ protected function getFiles(MessageInterface $message)
}

// Process files
$attachments = $this->processAttachments($message->getAttachments(), function ($name, AttachmentInterface $attachment) {
return $attachment->getPath();
});
list($attachments,) = $this->processAttachments($message->getAttachments());

// Format params
$files = array();
Expand Down Expand Up @@ -115,15 +114,7 @@ protected function format(MessageInterface $message)
$inline = array();
if ($message instanceof AttachmentsInterface) {
// Store inline attachment references
$this->processAttachments($message->getAttachments(), function ($name, AttachmentInterface $attachment) use (&$inline) {
$id = $attachment->getID();
if (isset($id)) {
// Reference inline
$inline[$id] = $name;
}

return $attachment->getPath();
});
list(,$inline) = $this->processAttachments($message->getAttachments());
}

$parameters = array(
Expand All @@ -148,4 +139,27 @@ protected function format(MessageInterface $message)

return http_build_query(array_filter($parameters));
}

/**
* @param AttachmentInterface[] $attachments
* @return array First element: All attachments – array(name => path). Second element: Inline attachments – array(id => name)
*/
protected function processAttachments(array $attachments)
{
$attachments = AttachmentUtils::processAttachments($attachments);

$processedAttachments = array();
$inline = array();
foreach ($attachments as $name => $attachment) {
$id = $attachment->getID();
if (isset($id)) {
// Reference inline
$inline[$id] = $name;
}

$processedAttachments[$name] = $attachment->getPath();
}

return array($processedAttachments, $inline);
}
}
16 changes: 3 additions & 13 deletions lib/Stampie/Util/AttachmentUtils.php
Expand Up @@ -15,17 +15,12 @@ abstract class AttachmentUtils
* Applies a function to each attachment, and finds a unique name for any conflicting names
*
* @param AttachmentInterface[] $attachments
* @param callable $callback function (string $name, AttachmentInterface $attachment) { ... }
* @return array
*
* @throws \InvalidArgumentException
*/
public static function processAttachments(array $attachments, $callback)
public static function processAttachments(array $attachments)
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException('Callback must be callable');
}

$processed = array();

foreach ($attachments as $attachment) {
Expand All @@ -40,12 +35,7 @@ public static function processAttachments(array $attachments, $callback)
$name = static::findUniqueName($name, array_keys($processed));
}

$result = $callback($name, $attachment);
if (!isset($result)) {
continue;
}

$processed[$name] = $result;
$processed[$name] = $attachment;
}

return $processed;
Expand All @@ -56,7 +46,7 @@ public static function processAttachments(array $attachments, $callback)
* @param array $claimed Names already in use to avoid
* @return string A unique name
*/
protected static function findUniqueName($name, array $claimed)
public static function findUniqueName($name, array $claimed)
{
$ext = pathinfo($name, \PATHINFO_EXTENSION);
$basename = substr($name, 0, -strlen('.'.$ext));
Expand Down

0 comments on commit dba21be

Please sign in to comment.