Skip to content

Commit

Permalink
Initial attachment logic (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamquaile authored Jul 7, 2017
1 parent 50581da commit 7cab04f
Show file tree
Hide file tree
Showing 21 changed files with 634 additions and 18 deletions.
4 changes: 4 additions & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ services:
- '@enveloper.template_language'
- '@enveloper.resolution.participant_list_resolver'
- '@enveloepr.resolution.participant_resolver'
- '@Outstack\Enveloper\Resolution\AttachmentListResolver'
- '%default_sender_email%'
- '%default_sender_name%'

Outstack\Enveloper\Resolution\AttachmentListResolver: ~
Outstack\Enveloper\Resolution\AttachmentResolver: ~


enveloper.templates.template_loader:
alias: enveloper.templates.template_loader.filesystem
Expand Down
12 changes: 10 additions & 2 deletions src/AppBundle/Controller/OutboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,17 @@ private function convertToSwiftMessage(Message $message)
->setFrom($swiftFrom)
->setTo($swiftTo)
->setCc($swiftCc)
->setBcc($swiftBcc)
->setBcc($swiftBcc);

foreach ($message->getAttachments() as $attachment) {
$swiftMessage->attach(
\Swift_Attachment::newInstance($attachment->getData(), $attachment->getFilename())
);
}
$swiftMessage
->setBody($message->getHtml(), 'text/html')
->addPart($message->getText(), 'text/plain');
->addPart($message->getText(), 'text/plain')
;

return $swiftMessage;

Expand Down
31 changes: 31 additions & 0 deletions src/Outstack/Enveloper/Mail/Attachments/Attachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Outstack\Enveloper\Mail\Attachments;

class Attachment
{
/**
* @var string
*/
private $data;
/**
* @var string
*/
private $filename;

public function __construct(string $data, string $filename)
{
$this->data = $data;
$this->filename = $filename;
}

public function getData(): string
{
return $this->data;
}

public function getFilename(): string
{
return $this->filename;
}
}
31 changes: 31 additions & 0 deletions src/Outstack/Enveloper/Mail/Attachments/AttachmentList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Outstack\Enveloper\Mail\Attachments;

class AttachmentList implements \IteratorAggregate
{
/**
* @var Attachment[]
*/
private $attachments = [];

public function __construct(array $attachments)
{
foreach ($attachments as $attachment) {
$this->addAttachment($attachment);
}
}

private function addAttachment(Attachment $attachment)
{
$this->attachments[] = $attachment;
}

/**
* @return \ArrayIterator|Attachment[]
*/
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->attachments);
}
}
33 changes: 27 additions & 6 deletions src/Outstack/Enveloper/Mail/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Outstack\Enveloper\Mail;

use Outstack\Enveloper\Mail\Attachments\AttachmentList;
use Outstack\Enveloper\Mail\Participants\Participant;
use Outstack\Enveloper\Mail\Participants\ParticipantList;

Expand Down Expand Up @@ -40,8 +41,22 @@ class Message
* @var Participant
*/
private $sender;
/**
* @var AttachmentList
*/
private $attachments;

public function __construct(string $id, string $subject, Participant $sender, ParticipantList $to, ParticipantList $cc, ParticipantList $bcc, string $text, string $html)
public function __construct(
string $id,
string $subject,
Participant $sender,
ParticipantList $to,
ParticipantList $cc,
ParticipantList $bcc,
string $text,
string $html,
AttachmentList $attachments
)
{
$this->id = $id;
$this->subject = $subject;
Expand All @@ -51,6 +66,7 @@ public function __construct(string $id, string $subject, Participant $sender, Pa
$this->cc = $cc;
$this->bcc = $bcc;
$this->html = $html;
$this->attachments = $attachments;
}

/**
Expand All @@ -77,11 +93,6 @@ public function getBcc(): ParticipantList
return $this->bcc;
}

public function getHtml(): string
{
return $this->html;
}

public function getId(): string
{
return $this->id;
Expand All @@ -101,4 +112,14 @@ public function getText(): string
{
return $this->text;
}

public function getHtml(): string
{
return $this->html;
}

public function getAttachments(): AttachmentList
{
return $this->attachments;
}
}
52 changes: 52 additions & 0 deletions src/Outstack/Enveloper/Resolution/AttachmentListResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Outstack\Enveloper\Resolution;

use Outstack\Enveloper\Mail\Attachments\AttachmentList;
use Outstack\Enveloper\Templates\AttachmentListTemplate;
use Outstack\Enveloper\Templates\AttachmentTemplate;

class AttachmentListResolver
{
/**
* @var AttachmentResolver
*/
private $attachmentResolver;

public function __construct(AttachmentResolver $attachmentResolver)
{
$this->attachmentResolver = $attachmentResolver;
}

public function resolveAttachmentList(AttachmentListTemplate $attachmentListTemplate, array $params): AttachmentList
{
$resolved = [];
foreach ($attachmentListTemplate->getAttachmentTemplates() as $template) {
foreach ($this->resolveTemplate($template, $params) as $attachment) {
$resolved[] = $attachment;
}
}

return new AttachmentList($resolved);
}

private function resolveTemplate(AttachmentTemplate $template, array $params)
{
if ($template->getIterateOver()) {
foreach ($this->resolveIteratively($template, $params) as $template) {
yield $template;
}

return;
}

yield $this->attachmentResolver->resolve($template, $params);
}

private function resolveIteratively(AttachmentTemplate $template, array $params)
{
foreach ($params[$template->getIterateOver()] as $item) {
yield $this->attachmentResolver->resolve($template, ['item' => $item]);
}
}
}
33 changes: 33 additions & 0 deletions src/Outstack/Enveloper/Resolution/AttachmentResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Outstack\Enveloper\Resolution;

use Outstack\Enveloper\Mail\Attachments\Attachment;
use Outstack\Enveloper\Templates\AttachmentTemplate;

class AttachmentResolver
{
/**
* @var TemplateLanguage
*/
private $language;

public function __construct(TemplateLanguage $language)
{
$this->language = $language;
}

public function resolve(AttachmentTemplate $template, array $parameters)
{
return new Attachment(
$this->language->render(
$template->getContents(),
$parameters
),
$this->language->render(
$template->getFilename(),
$parameters
)
);
}
}
19 changes: 16 additions & 3 deletions src/Outstack/Enveloper/Resolution/MessageResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Outstack\Enveloper\Resolution;

use Outstack\Enveloper\Mail\Attachments\AttachmentList;
use Outstack\Enveloper\Mail\Message;
use Outstack\Enveloper\Mail\Participants\EmailAddress;
use Outstack\Enveloper\Mail\Participants\Participant;
Expand All @@ -21,6 +22,10 @@ class MessageResolver
* @var ParticipantResolver
*/
private $recipientResolver;
/**
* @var AttachmentListResolver
*/
private $attachmentListResolver;
/**
* @var string
*/
Expand All @@ -30,13 +35,20 @@ class MessageResolver
*/
private $defaultSenderName;

public function __construct(TemplateLanguage $language, ParticipantListResolver $recipientListResolver, ParticipantResolver $recipientResolver, ?string $defaultSenderEmail, ?string $defaultSenderName)
{
public function __construct(
TemplateLanguage $language,
ParticipantListResolver $recipientListResolver,
ParticipantResolver $recipientResolver,
AttachmentListResolver $attachmentListResolver,
?string $defaultSenderEmail,
?string $defaultSenderName
) {
$this->language = $language;
$this->recipientListResolver = $recipientListResolver;
$this->recipientResolver = $recipientResolver;
$this->defaultSenderEmail = $defaultSenderEmail;
$this->defaultSenderName = $defaultSenderName;
$this->attachmentListResolver = $attachmentListResolver;
}

public function resolve(Template $template, $parameters): Message
Expand All @@ -55,7 +67,8 @@ public function resolve(Template $template, $parameters): Message
$this->recipientListResolver->resolveParticipantList($template->getRecipientsCc(), $parameters),
$this->recipientListResolver->resolveParticipantList($template->getRecipientsBcc(), $parameters),
$this->language->render($template->getText(), $parameters),
$this->language->render($template->getHtml(), $parameters)
$this->language->render($template->getHtml(), $parameters),
$this->attachmentListResolver->resolveAttachmentList($template->getAttachments(), $parameters)
);
}
}
31 changes: 31 additions & 0 deletions src/Outstack/Enveloper/Templates/AttachmentListTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Outstack\Enveloper\Templates;

class AttachmentListTemplate
{
/**
* @var AttachmentTemplate[]
*/
private $templates = [];

public function __construct(array $attachmentTemplates)
{
foreach ($attachmentTemplates as $template) {
$this->addTemplate($template);
}
}

private function addTemplate(AttachmentTemplate $template)
{
$this->templates[] = $template;
}

/**
* @return AttachmentTemplate[]
*/
public function getAttachmentTemplates(): array
{
return $this->templates;
}
}
41 changes: 41 additions & 0 deletions src/Outstack/Enveloper/Templates/AttachmentTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Outstack\Enveloper\Templates;

class AttachmentTemplate
{
/**
* @var string
*/
private $contents;
/**
* @var string
*/
private $filename;
/**
* @var null|string
*/
private $iterateOver;

public function __construct(string $contents, string $filename, ?string $iterateOver = null)
{
$this->contents = $contents;
$this->filename = $filename;
$this->iterateOver = $iterateOver;
}

public function getContents(): string
{
return $this->contents;
}

public function getFilename(): string
{
return $this->filename;
}

public function getIterateOver(): ?string
{
return $this->iterateOver;
}
}
Loading

0 comments on commit 7cab04f

Please sign in to comment.