Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ refactor for postal/postal v2 #34

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
{
"name": "synergitech/laravel-postal",
"description": "This library integrates Postal with the standard Laravel mail framework.",
"keywords": [
"Laravel",
"Postal",
"Email"
],
"homepage": "https://github.com/synergitech/laravel-postal",
"license": "MIT",
"require": {
"php": "^8.0",
"laravel/framework": "^9.0.1|^10.0",
"postal/postal": "^1.0.1"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": "^7.0|^8.0",
"nunomaduro/larastan": "^2.0"
},
"autoload": {
"psr-4": {
"SynergiTech\\Postal\\": "src",
"SynergiTech\\Postal\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"SynergiTech\\Postal\\PostalServiceProvider"
]
}
},
"scripts": {
"test": [
"phpunit",
"phpstan --memory-limit=1G"
]
}
}
{
"name": "synergitech/laravel-postal",
"description": "This library integrates Postal with the standard Laravel mail framework.",
"keywords": [
"Laravel",
"Postal",
"Email"
],
"homepage": "https://github.com/synergitech/laravel-postal",
"license": "MIT",
"require": {
"php": "^8.0",
"laravel/framework": "^9.0.1|^10.0",
"postal/postal": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": "^7.0|^8.0",
"nunomaduro/larastan": "^2.0"
},
"autoload": {
"psr-4": {
"SynergiTech\\Postal\\": "src",
"SynergiTech\\Postal\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"SynergiTech\\Postal\\PostalServiceProvider"
]
}
},
"scripts": {
"test": [
"phpunit",
"phpstan --memory-limit=1G"
]
}
}
14 changes: 9 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ parameters:
count: 1
path: src/PostalNotificationChannel.php

-
message: "#^Access to an undefined property Postal\\\\SendResult\\:\\:\\$result\\.$#"
count: 1
path: src/PostalTransport.php

-
message: "#^Access to an undefined property object\\:\\:\\$body\\.$#"
count: 2
Expand Down Expand Up @@ -95,3 +90,12 @@ parameters:
count: 1
path: src/PostalTransport.php

-
message: "#^Parameter \\#1 \\$content of method Postal\\\\Send\\\\Message\\:\\:htmlBody\\(\\) expects string, resource\\|string given\\.$#"
count: 1
path: src/PostalTransport.php

-
message: "#^Parameter \\#1 \\$content of method Postal\\\\Send\\\\Message\\:\\:plainBody\\(\\) expects string, resource\\|string given\\.$#"
count: 1
path: src/PostalTransport.php
2 changes: 1 addition & 1 deletion src/PostalNotificationChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PostalNotificationChannel extends MailChannel
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
* @return \Illuminate\Mail\SentMessage|null
*/
public function send($notifiable, Notification $notification)
{
Expand Down
13 changes: 10 additions & 3 deletions src/PostalServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ public function boot(): void
}

Mail::extend('postal', function (array $config = []) {
$config = config('postal', []);
$config = config('postal');

if (! is_array($config)) {
$config = [];
throw new \RuntimeException('missing Postal configuration');
}
if (! is_string($config['domain'])) {
throw new \RuntimeException('missing Postal domain configuration');
}
if (! is_string($config['key'])) {
throw new \RuntimeException('missing Postal key configuration');
}

return new PostalTransport(new Client($config['domain'] ?? null, $config['key'] ?? null));
return new PostalTransport(new Client($config['domain'], $config['key']));
});
}
}
28 changes: 13 additions & 15 deletions src/PostalTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace SynergiTech\Postal;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Postal\Client;
use Postal\Error;
use Postal\SendMessage;
use Postal\SendResult;
use Postal\ApiException;
use Postal\Send\Message as SendMessage;
use Postal\Send\Result as SendResult;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
Expand Down Expand Up @@ -37,16 +35,16 @@ protected function doSend(SentMessage $sentMessage): void
$postalmessage = $this->symfonyToPostal($symfonyMessage);

try {
$response = $postalmessage->send();
} catch (Error $error) {
$response = $this->client->send->message($postalmessage);
} catch (ApiException $error) {
throw new TransportException($error->getMessage(), $error->getCode(), $error);
}

$headers = $symfonyMessage->getHeaders();

// send known header back for laravel to match emails coming out of Postal
// - doesn't seem we can replace Message-ID
$headers->addTextHeader('Postal-Message-ID', $response->result->message_id);
$headers->addTextHeader('Postal-Message-ID', $response->message_id);

if (config('postal.enable.emaillogging') !== true) {
return;
Expand All @@ -61,7 +59,7 @@ protected function doSend(SentMessage $sentMessage): void
if ($emailable_type != '' && $emailable_id != '') {
$emailmodel = config('postal.models.email');
\DB::table((new $emailmodel)->getTable())
->where('postal_email_id', $response->result->message_id)
->where('postal_email_id', $response->message_id)
->update([
'emailable_type' => $emailable_type,
'emailable_id' => $emailable_id,
Expand Down Expand Up @@ -107,13 +105,13 @@ private function symfonyToPostal(Email $symfonyMessage): SendMessage
$postalMessage->htmlBody($symfonyMessage->getHtmlBody());
}

foreach ($symfonyMessage->getAttachments() as $symfonyPart) {
foreach ($symfonyMessage->getAttachments() as $index => $symfonyPart) {
$filename = $symfonyPart
->getPreparedHeaders()
->getHeaderParameter('content-disposition', 'filename');

$postalMessage->attach(
$filename,
$filename ?? "attached_file_$index",
$symfonyPart->getMediaType() . '/' . $symfonyPart->getMediaSubtype(),
$symfonyPart->getBody()
);
Expand Down Expand Up @@ -162,9 +160,9 @@ private function recordEmailsFromResponse(Email $symfonyMessage, SendResult $res
$email->body = $symfonyMessage->getHtmlBody();
}

$email->postal_email_id = $response->result->message_id;
$email->postal_id = $message->id();
$email->postal_token = $message->token();
$email->postal_email_id = $response->message_id;
$email->postal_id = $message->id;
$email->postal_token = $message->token;

$email->save();
}
Expand All @@ -181,7 +179,7 @@ private function stringifyAddress(Address $address): string

private function getNewSendMessage(): SendMessage
{
return new SendMessage($this->client);
return new SendMessage();
}

/**
Expand Down
92 changes: 53 additions & 39 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Postal\Client;
use Postal\Send\Result;
use Postal\SendService;
use Symfony\Component\Mailer\DelayedEnvelope;
use SynergiTech\Postal\PostalNotificationChannel;
use SynergiTech\Postal\PostalTransport;
Expand All @@ -13,19 +15,23 @@ class FeatureTest extends TestCase
{
public function testSendingNotificationOnDemand()
{
$result = new \stdClass;
$result->message_id = 'feature-test';
$message = new \stdClass();
$message->id = 'feature-test';
$message->token = 'feature-test';
$result->messages['[email protected]'] = $message;

$clientMock = $this->createMock(Client::class);
$clientMock
->method('makeRequest')
->willReturn($result);

Mail::extend('postal', function (array $config = []) use ($clientMock) {
Mail::extend('postal', function (array $config = []) {
$clientMock = $this->createMock(Client::class);
$serviceMock = $this->createMock(SendService::class);

$result = new Result([
'message_id' => 'feature-test',
'messages' => ['[email protected]' => [
'id' => 123,
'token' => 'feature-test',
]],
]);

$serviceMock->method('message')
->willReturn($result);

$clientMock->send = $serviceMock;

return new PostalTransport($clientMock);
});

Expand All @@ -48,19 +54,23 @@ public function testSendingNotificationOnDemand()

public function testSendingNotificationOnDemandWithAlias()
{
$result = new \stdClass;
$result->message_id = 'feature-test';
$message = new \stdClass();
$message->id = 'feature-test';
$message->token = 'feature-test';
$result->messages['[email protected]'] = $message;

$clientMock = $this->createMock(Client::class);
$clientMock
->method('makeRequest')
->willReturn($result);

Mail::extend('postal', function (array $config = []) use ($clientMock) {
Mail::extend('postal', function (array $config = []) {
$clientMock = $this->createMock(Client::class);
$serviceMock = $this->createMock(SendService::class);

$result = new Result([
'message_id' => 'feature-test',
'messages' => ['[email protected]' => [
'id' => 123,
'token' => 'feature-test',
]],
]);

$serviceMock->method('message')
->willReturn($result);

$clientMock->send = $serviceMock;

return new PostalTransport($clientMock);
});

Expand All @@ -83,19 +93,23 @@ public function testSendingNotificationOnDemandWithAlias()

public function testSendingNotificationWithNotifiableTrait()
{
$result = new \stdClass;
$result->message_id = 'feature-test';
$message = new \stdClass();
$message->id = 'feature-test';
$message->token = 'feature-test';
$result->messages['[email protected]'] = $message;

$clientMock = $this->createMock(Client::class);
$clientMock
->method('makeRequest')
->willReturn($result);

Mail::extend('postal', function (array $config = []) use ($clientMock) {
Mail::extend('postal', function (array $config = []) {
$clientMock = $this->createMock(Client::class);
$serviceMock = $this->createMock(SendService::class);

$result = new Result([
'message_id' => 'feature-test',
'messages' => ['[email protected]' => [
'id' => 123,
'token' => 'feature-test',
]],
]);

$serviceMock->method('message')
->willReturn($result);

$clientMock->send = $serviceMock;

return new PostalTransport($clientMock);
});

Expand Down
4 changes: 4 additions & 0 deletions tests/PostalServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public function testBoot()
{
// assert the service provider did boot and extend the mail

config([
'postal' => ['domain' => 'example.com', 'key' => 'hunter2'],
]);

$driver = app('mail.manager')
->createSymfonyTransport(config('mail.mailers.postal'));

Expand Down
Loading