Skip to content

Commit bbacb97

Browse files
committed
♻️ refactor for postal/postal v2
1 parent bc5c219 commit bbacb97

8 files changed

+194
-158
lines changed

composer.json

+40-40
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
{
2-
"name": "synergitech/laravel-postal",
3-
"description": "This library integrates Postal with the standard Laravel mail framework.",
4-
"keywords": [
5-
"Laravel",
6-
"Postal",
7-
"Email"
8-
],
9-
"homepage": "https://github.com/synergitech/laravel-postal",
10-
"license": "MIT",
11-
"require": {
12-
"php": "^8.0",
13-
"laravel/framework": "^9.0.1|^10.0",
14-
"postal/postal": "^1.0.1"
15-
},
16-
"require-dev": {
17-
"phpunit/phpunit": "^9.0",
18-
"orchestra/testbench": "^7.0|^8.0",
19-
"nunomaduro/larastan": "^2.0"
20-
},
21-
"autoload": {
22-
"psr-4": {
23-
"SynergiTech\\Postal\\": "src",
24-
"SynergiTech\\Postal\\Tests\\": "tests"
25-
}
26-
},
27-
"extra": {
28-
"laravel": {
29-
"providers": [
30-
"SynergiTech\\Postal\\PostalServiceProvider"
31-
]
32-
}
33-
},
34-
"scripts": {
35-
"test": [
36-
"phpunit",
37-
"phpstan --memory-limit=1G"
38-
]
39-
}
40-
}
1+
{
2+
"name": "synergitech/laravel-postal",
3+
"description": "This library integrates Postal with the standard Laravel mail framework.",
4+
"keywords": [
5+
"Laravel",
6+
"Postal",
7+
"Email"
8+
],
9+
"homepage": "https://github.com/synergitech/laravel-postal",
10+
"license": "MIT",
11+
"require": {
12+
"php": "^8.0",
13+
"laravel/framework": "^9.0.1|^10.0",
14+
"postal/postal": "^2.0"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^9.0",
18+
"orchestra/testbench": "^7.0|^8.0",
19+
"nunomaduro/larastan": "^2.0"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"SynergiTech\\Postal\\": "src",
24+
"SynergiTech\\Postal\\Tests\\": "tests"
25+
}
26+
},
27+
"extra": {
28+
"laravel": {
29+
"providers": [
30+
"SynergiTech\\Postal\\PostalServiceProvider"
31+
]
32+
}
33+
},
34+
"scripts": {
35+
"test": [
36+
"phpunit",
37+
"phpstan --memory-limit=1G"
38+
]
39+
}
40+
}

phpstan-baseline.neon

+9-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ parameters:
3535
count: 1
3636
path: src/PostalNotificationChannel.php
3737

38-
-
39-
message: "#^Access to an undefined property Postal\\\\SendResult\\:\\:\\$result\\.$#"
40-
count: 1
41-
path: src/PostalTransport.php
42-
4338
-
4439
message: "#^Access to an undefined property object\\:\\:\\$body\\.$#"
4540
count: 2
@@ -95,3 +90,12 @@ parameters:
9590
count: 1
9691
path: src/PostalTransport.php
9792

93+
-
94+
message: "#^Parameter \\#1 \\$content of method Postal\\\\Send\\\\Message\\:\\:htmlBody\\(\\) expects string, resource\\|string given\\.$#"
95+
count: 1
96+
path: src/PostalTransport.php
97+
98+
-
99+
message: "#^Parameter \\#1 \\$content of method Postal\\\\Send\\\\Message\\:\\:plainBody\\(\\) expects string, resource\\|string given\\.$#"
100+
count: 1
101+
path: src/PostalTransport.php

src/PostalNotificationChannel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class PostalNotificationChannel extends MailChannel
1313
*
1414
* @param mixed $notifiable
1515
* @param \Illuminate\Notifications\Notification $notification
16-
* @return void
16+
* @return \Illuminate\Mail\SentMessage|null
1717
*/
1818
public function send($notifiable, Notification $notification)
1919
{

src/PostalServiceProvider.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,19 @@ public function boot(): void
3131
}
3232

3333
Mail::extend('postal', function (array $config = []) {
34-
$config = config('postal', []);
34+
$config = config('postal');
35+
3536
if (! is_array($config)) {
36-
$config = [];
37+
throw new \RuntimeException('missing Postal configuration');
38+
}
39+
if (! is_string($config['domain'])) {
40+
throw new \RuntimeException('missing Postal domain configuration');
41+
}
42+
if (! is_string($config['key'])) {
43+
throw new \RuntimeException('missing Postal key configuration');
3744
}
3845

39-
return new PostalTransport(new Client($config['domain'] ?? null, $config['key'] ?? null));
46+
return new PostalTransport(new Client($config['domain'], $config['key']));
4047
});
4148
}
4249
}

src/PostalTransport.php

+13-15
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace SynergiTech\Postal;
44

5-
use Illuminate\Database\Eloquent\Model;
6-
use Illuminate\Support\Facades\DB;
75
use Postal\Client;
8-
use Postal\Error;
9-
use Postal\SendMessage;
10-
use Postal\SendResult;
6+
use Postal\ApiException;
7+
use Postal\Send\Message as SendMessage;
8+
use Postal\Send\Result as SendResult;
119
use Symfony\Component\Mailer\Exception\TransportException;
1210
use Symfony\Component\Mailer\SentMessage;
1311
use Symfony\Component\Mailer\Transport\AbstractTransport;
@@ -37,16 +35,16 @@ protected function doSend(SentMessage $sentMessage): void
3735
$postalmessage = $this->symfonyToPostal($symfonyMessage);
3836

3937
try {
40-
$response = $postalmessage->send();
41-
} catch (Error $error) {
38+
$response = $this->client->send->message($postalmessage);
39+
} catch (ApiException $error) {
4240
throw new TransportException($error->getMessage(), $error->getCode(), $error);
4341
}
4442

4543
$headers = $symfonyMessage->getHeaders();
4644

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

5149
if (config('postal.enable.emaillogging') !== true) {
5250
return;
@@ -61,7 +59,7 @@ protected function doSend(SentMessage $sentMessage): void
6159
if ($emailable_type != '' && $emailable_id != '') {
6260
$emailmodel = config('postal.models.email');
6361
\DB::table((new $emailmodel)->getTable())
64-
->where('postal_email_id', $response->result->message_id)
62+
->where('postal_email_id', $response->message_id)
6563
->update([
6664
'emailable_type' => $emailable_type,
6765
'emailable_id' => $emailable_id,
@@ -107,13 +105,13 @@ private function symfonyToPostal(Email $symfonyMessage): SendMessage
107105
$postalMessage->htmlBody($symfonyMessage->getHtmlBody());
108106
}
109107

110-
foreach ($symfonyMessage->getAttachments() as $symfonyPart) {
108+
foreach ($symfonyMessage->getAttachments() as $index => $symfonyPart) {
111109
$filename = $symfonyPart
112110
->getPreparedHeaders()
113111
->getHeaderParameter('content-disposition', 'filename');
114112

115113
$postalMessage->attach(
116-
$filename,
114+
$filename ?? "attached_file_$index",
117115
$symfonyPart->getMediaType() . '/' . $symfonyPart->getMediaSubtype(),
118116
$symfonyPart->getBody()
119117
);
@@ -162,9 +160,9 @@ private function recordEmailsFromResponse(Email $symfonyMessage, SendResult $res
162160
$email->body = $symfonyMessage->getHtmlBody();
163161
}
164162

165-
$email->postal_email_id = $response->result->message_id;
166-
$email->postal_id = $message->id();
167-
$email->postal_token = $message->token();
163+
$email->postal_email_id = $response->message_id;
164+
$email->postal_id = $message->id;
165+
$email->postal_token = $message->token;
168166

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

182180
private function getNewSendMessage(): SendMessage
183181
{
184-
return new SendMessage($this->client);
182+
return new SendMessage();
185183
}
186184

187185
/**

tests/FeatureTest.php

+53-39
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Support\Facades\Mail;
66
use Illuminate\Support\Facades\Notification;
77
use Postal\Client;
8+
use Postal\Send\Result;
9+
use Postal\SendService;
810
use Symfony\Component\Mailer\DelayedEnvelope;
911
use SynergiTech\Postal\PostalNotificationChannel;
1012
use SynergiTech\Postal\PostalTransport;
@@ -13,19 +15,23 @@ class FeatureTest extends TestCase
1315
{
1416
public function testSendingNotificationOnDemand()
1517
{
16-
$result = new \stdClass;
17-
$result->message_id = 'feature-test';
18-
$message = new \stdClass();
19-
$message->id = 'feature-test';
20-
$message->token = 'feature-test';
21-
$result->messages['[email protected]'] = $message;
22-
23-
$clientMock = $this->createMock(Client::class);
24-
$clientMock
25-
->method('makeRequest')
26-
->willReturn($result);
27-
28-
Mail::extend('postal', function (array $config = []) use ($clientMock) {
18+
Mail::extend('postal', function (array $config = []) {
19+
$clientMock = $this->createMock(Client::class);
20+
$serviceMock = $this->createMock(SendService::class);
21+
22+
$result = new Result([
23+
'message_id' => 'feature-test',
24+
'messages' => ['[email protected]' => [
25+
'id' => 123,
26+
'token' => 'feature-test',
27+
]],
28+
]);
29+
30+
$serviceMock->method('message')
31+
->willReturn($result);
32+
33+
$clientMock->send = $serviceMock;
34+
2935
return new PostalTransport($clientMock);
3036
});
3137

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

4955
public function testSendingNotificationOnDemandWithAlias()
5056
{
51-
$result = new \stdClass;
52-
$result->message_id = 'feature-test';
53-
$message = new \stdClass();
54-
$message->id = 'feature-test';
55-
$message->token = 'feature-test';
56-
$result->messages['[email protected]'] = $message;
57-
58-
$clientMock = $this->createMock(Client::class);
59-
$clientMock
60-
->method('makeRequest')
61-
->willReturn($result);
62-
63-
Mail::extend('postal', function (array $config = []) use ($clientMock) {
57+
Mail::extend('postal', function (array $config = []) {
58+
$clientMock = $this->createMock(Client::class);
59+
$serviceMock = $this->createMock(SendService::class);
60+
61+
$result = new Result([
62+
'message_id' => 'feature-test',
63+
'messages' => ['[email protected]' => [
64+
'id' => 123,
65+
'token' => 'feature-test',
66+
]],
67+
]);
68+
69+
$serviceMock->method('message')
70+
->willReturn($result);
71+
72+
$clientMock->send = $serviceMock;
73+
6474
return new PostalTransport($clientMock);
6575
});
6676

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

8494
public function testSendingNotificationWithNotifiableTrait()
8595
{
86-
$result = new \stdClass;
87-
$result->message_id = 'feature-test';
88-
$message = new \stdClass();
89-
$message->id = 'feature-test';
90-
$message->token = 'feature-test';
91-
$result->messages['[email protected]'] = $message;
92-
93-
$clientMock = $this->createMock(Client::class);
94-
$clientMock
95-
->method('makeRequest')
96-
->willReturn($result);
97-
98-
Mail::extend('postal', function (array $config = []) use ($clientMock) {
96+
Mail::extend('postal', function (array $config = []) {
97+
$clientMock = $this->createMock(Client::class);
98+
$serviceMock = $this->createMock(SendService::class);
99+
100+
$result = new Result([
101+
'message_id' => 'feature-test',
102+
'messages' => ['[email protected]' => [
103+
'id' => 123,
104+
'token' => 'feature-test',
105+
]],
106+
]);
107+
108+
$serviceMock->method('message')
109+
->willReturn($result);
110+
111+
$clientMock->send = $serviceMock;
112+
99113
return new PostalTransport($clientMock);
100114
});
101115

tests/PostalServiceProviderTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public function testBoot()
1010
{
1111
// assert the service provider did boot and extend the mail
1212

13+
config([
14+
'postal' => ['domain' => 'example.com', 'key' => 'hunter2'],
15+
]);
16+
1317
$driver = app('mail.manager')
1418
->createSymfonyTransport(config('mail.mailers.postal'));
1519

0 commit comments

Comments
 (0)