Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
micc83 committed Aug 25, 2020
1 parent ded1157 commit c3ad9ec
Show file tree
Hide file tree
Showing 27 changed files with 621 additions and 114 deletions.
3 changes: 2 additions & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ $finder = PhpCsFixer\Finder::create()
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'strict_param' => false,
'strict_param' => true,
'declare_strict_types' => true,
'array_syntax' => ['syntax' => 'short'],
])
->setFinder($finder);
2 changes: 1 addition & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
'websocket' => [
'host' => '127.0.0.1:1338',
],
];
];
5 changes: 4 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Config
private array $params;
private ?array $altParams;

const VERSION = "1.0.0";
const DATE_FORMAT = "Y-m-d H:i:s";

public function __construct(array $params, array $altParams = null)
{
$this->params = $params;
Expand Down Expand Up @@ -52,4 +55,4 @@ private static function dotGet(string $key, array $data)

return $data;
}
}
}
28 changes: 24 additions & 4 deletions src/Emails/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

class Attachment
{
public string $filename;
public string $content;
public string $type;
public string $id;
private string $filename;
private string $content;
private string $type;
private string $id;

public function __construct(string $filename, string $content, string $type)
{
Expand All @@ -16,4 +16,24 @@ public function __construct(string $filename, string $content, string $type)
$this->content = $content;
$this->type = $type;
}

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

public function getContent(): string
{
return $this->content;
}

public function getType(): string
{
return $this->type;
}

public function getId(): string
{
return $this->id;
}
}
86 changes: 65 additions & 21 deletions src/Emails/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@

namespace Mailamie\Emails;

use DateTime;
use DateTimeImmutable;
use Exception;
use Mailamie\Config;

/**
* Class Message
* @package Mailamie\Emails
*/
class Message
{
private string $raw;
public string $id;
public string $sender;
public array $recipients;
public array $ccs;
public string $htmlBody;
public string $textBody;
public string $subject;
public DateTime $created_at;
private string $id;
private string $sender;
/** @var string[] */
private array $recipients;
/** @var string[] */
private array $ccs;
private string $htmlBody;
private string $textBody;
private string $subject;
private DateTimeImmutable $created_at;
private string $replyTo;
/** @var string[] */
private array $allRecipients;
/** @var Attachment[] */
private array $attachments;

public function __construct(
Expand All @@ -31,8 +40,7 @@ public function __construct(
string $replyTo,
array $allRecipients,
array $attachments
)
{
) {
$this->id = (string)uniqid();
$this->raw = $raw;
$this->sender = $sender;
Expand All @@ -41,7 +49,7 @@ public function __construct(
$this->htmlBody = $htmlBody;
$this->textBody = $textBody;
$this->subject = $subject;
$this->created_at = new DateTime();
$this->created_at = new DateTimeImmutable();
$this->replyTo = $replyTo;
$this->allRecipients = $allRecipients;
$this->attachments = $attachments;
Expand All @@ -50,7 +58,7 @@ public function __construct(
public function getAttachment(string $id): Attachment
{
$attachments = array_values(array_filter($this->attachments, function (Attachment $attachment) use ($id) {
return $attachment->id === $id;
return $attachment->getId() === $id;
}));

if (!count($attachments)) {
Expand All @@ -65,10 +73,46 @@ public function getExcerpt()
return mb_strimwidth(strip_tags($this->htmlBody) ?: $this->textBody, 0, 30);
}

public function getId(): string
{
return $this->id;
}

public function getSender(): string
{
return $this->sender;
}

/**
* @return string[]
*/
public function getRecipients(): array
{
return $this->recipients;
}

public function getSubject(): string
{
return $this->subject;
}

public function getCreatedAt(): DateTimeImmutable
{
return $this->created_at;
}

/**
* @return Attachment[]
*/
public function getAttachments(): array
{
return $this->attachments;
}

public function toTable()
{
$table = [
['Date', $this->created_at->format('Y-m-d H:i:s')],
['Date', $this->created_at->format(Config::DATE_FORMAT)],
['Subject', "<options=bold>{$this->subject}</>"],
['Excerpt', $this->getExcerpt()],
['To', implode("; ", $this->recipients)],
Expand Down Expand Up @@ -109,26 +153,26 @@ public function toArray(): array
'html' => $this->htmlBody,
'text' => $this->textBody,
'raw' => $this->raw,
'attachments' => $this->getAttachments(),
'created_at' => $this->created_at->format('Y-m-d H:i:s')
'attachments' => $this->attachmentsToArray(),
'created_at' => $this->created_at->format(Config::DATE_FORMAT)
];
}

private function getAttachments()
private function attachmentsToArray()
{
return array_map(function (Attachment $attachment) {
return [
'id' => $attachment->id,
'name' => $attachment->filename,
'url' => "/api/messages/{$this->id}/attachments/{$attachment->id}"
'id' => $attachment->getId(),
'name' => $attachment->getFilename(),
'url' => "/api/messages/{$this->id}/attachments/{$attachment->getId()}"
];
}, $this->attachments);
}

private function getAttachmentNames()
{
return array_map(function (Attachment $attachment) {
return $attachment->filename;
return $attachment->getFilename();
}, $this->attachments);
}

Expand Down
64 changes: 34 additions & 30 deletions src/Emails/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,15 @@ public function parse(string $rawContent, array $allRecipients = []): Message
$message = ParseMessage::from($rawContent);

$from = $message->getHeader('from')->getRawValue();

$recipients = array_map(function (AddressPart $addressPart) {
$name = $addressPart->getName();
$email = $addressPart->getValue();
if ($name) {
return "{$name} <{$email}>";
}
return $email;
}, $message->getHeader('to')->getAddresses());

$ccs = array_map(function (AddressPart $addressPart) {
$name = $addressPart->getName();
$email = $addressPart->getValue();
if ($name) {
return "{$name} <{$email}>";
}
return $email;
}, $message->getHeader('cc')->getAddresses());

$recipients = $this->joinNameAndEmail($message->getHeader('to')->getAddresses());
$ccs = $this->joinNameAndEmail($message->getHeader('cc')->getAddresses());
$subject = $message->getHeaderValue('subject');

$html = $message->getHtmlContent();
$text = $message->getTextContent();

$replyTo = $message->getHeader('reply-to')->getRawValue();

$attachments = [];
foreach ($message->getAllAttachmentParts() as $part) {
$attachments[] = new Attachment(
$part->getFilename(),
$part->getContent(),
$part->getContentType()
);
}
$attachments = $this->buildAttachmentFrom(
$message->getAllAttachmentParts()
);

return new Message(
$rawContent,
Expand All @@ -66,4 +41,33 @@ public function parse(string $rawContent, array $allRecipients = []): Message
$attachments
);
}

/**
* @param MessagePart[] $attachments
* @return Attachment[]
*/
private function buildAttachmentFrom(array $attachments): array
{
return array_map(function (MessagePart $part) {
return new Attachment(
$part->getFilename(),
$part->getContent(),
$part->getContentType()
);
}, $attachments);
}

/**
* @param AddressPart[] $addresses
* @return string[]
*/
private function joinNameAndEmail(array $addresses): array
{
return array_map(function (AddressPart $addressPart) {
$name = $addressPart->getName();
$email = $addressPart->getValue();

return $name ? "{$name} <{$email}>" : $email;
}, $addresses);
}
}
15 changes: 8 additions & 7 deletions src/Emails/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Exception;
use Mailamie\Config;

class Store
{
Expand All @@ -19,7 +20,7 @@ class Store

public function store(Message $message): void
{
$this->messages[$message->id] = $message;
$this->messages[$message->getId()] = $message;

foreach ($this->callbacks as $callback) {
$callback($message);
Expand All @@ -42,11 +43,11 @@ public function all(): array
{
return array_values(array_map(function (Message $message) {
return [
'id' => $message->id,
'from' => $message->sender,
'recipients' => $message->recipients,
'subject' => $message->subject,
'created_at' => $message->created_at->format('Y-m-d H:i:s')
'id' => $message->getId(),
'from' => $message->getSender(),
'recipients' => $message->getRecipients(),
'subject' => $message->getSubject(),
'created_at' => $message->getCreatedAt()->format(Config::DATE_FORMAT)
];
}, $this->sortedByDate()));
}
Expand All @@ -59,7 +60,7 @@ private function sortedByDate(): array
$messages = $this->messages;

usort($messages, function (Message $a, Message $b) {
return $a->created_at->getTimestamp() < $b->created_at->getTimestamp() ? 1 : -1;
return $a->getCreatedAt()->getTimestamp() < $b->getCreatedAt()->getTimestamp() ? 1 : -1;
});

return $messages;
Expand Down
6 changes: 3 additions & 3 deletions src/Events/DebugEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Mailamie\Events;

class DebugEvent
class DebugEvent implements Event
{
public $param;
public string $param;

public function __construct($param)
public function __construct(string $param)
{
$this->param = $param;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Mailamie\Events;

interface Event
{
}
4 changes: 2 additions & 2 deletions src/Events/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Mailamie\Events;

class Message
class Message implements Event
{
public string $body;

/**
* @var string[] $recipients
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Events/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mailamie\Events;

class Request
class Request implements Event
{
public string $body;

Expand Down
Loading

0 comments on commit c3ad9ec

Please sign in to comment.