Skip to content

Commit

Permalink
unique emails
Browse files Browse the repository at this point in the history
  • Loading branch information
gam6itko committed Jan 16, 2025
1 parent ee972a2 commit cb9044d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
34 changes: 25 additions & 9 deletions src/Sender/MailToFileSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Buggregator\Trap\Proto\Frame\Smtp;
use Buggregator\Trap\Sender;
use Buggregator\Trap\Support\FileSystem;
use Buggregator\Trap\Traffic\Message;
use Buggregator\Trap\Traffic\Message\Smtp\Contact;

/**
* @internal
Expand All @@ -31,11 +33,8 @@ public function send(iterable $frames): void
continue;
}

foreach ($frame->message->getBcc() as $bcc) {
$email = self::normalizeEmail($bcc->email);
if ($email === null) {
continue;
}
foreach ($this->collectUniqueEmails($frame->message) as $email) {
$email = self::normalizeEmail($email);

$path = $this->path . DIRECTORY_SEPARATOR . $email;
FileSystem::mkdir($path);
Expand All @@ -50,11 +49,28 @@ public function send(iterable $frames): void
/**
* Get normalized email address for file or directory name.
*
* @return null|non-empty-string
* @return non-empty-string

Check failure on line 52 in src/Sender/MailToFileSender.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

MoreSpecificReturnType

src/Sender/MailToFileSender.php:52:16: MoreSpecificReturnType: The declared return type 'non-empty-string' for Buggregator\Trap\Sender\MailToFileSender::normalizeEmail is more specific than the inferred return type 'string' (see https://psalm.dev/070)
*/
private static function normalizeEmail(?string $email): ?string
private static function normalizeEmail(string $email): string
{
$email = \str_replace('@', '[at]', \trim((string) $email));
return $email === '' ? null : $email;
return \str_replace('@', '[at]', \trim($email));

Check failure on line 56 in src/Sender/MailToFileSender.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

LessSpecificReturnStatement

src/Sender/MailToFileSender.php:56:16: LessSpecificReturnStatement: The type 'string' is more general than the declared return type 'non-empty-string' for Buggregator\Trap\Sender\MailToFileSender::normalizeEmail (see https://psalm.dev/129)
}

/**
* @return list<non-empty-string>

Check failure on line 60 in src/Sender/MailToFileSender.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

MoreSpecificReturnType

src/Sender/MailToFileSender.php:60:16: MoreSpecificReturnType: The declared return type 'list<non-empty-string>' for Buggregator\Trap\Sender\MailToFileSender::collectUniqueEmails is more specific than the inferred return type 'array<array-key, null|string>' (see https://psalm.dev/070)
*/
private function collectUniqueEmails(Message\Smtp $message): array
{
$fn = static fn(Contact $c) => $c->email;

Check failure on line 64 in src/Sender/MailToFileSender.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

MissingClosureReturnType

src/Sender/MailToFileSender.php:64:15: MissingClosureReturnType: Closure does not have a return type, expecting null|string (see https://psalm.dev/068)

return \array_unique(

Check failure on line 66 in src/Sender/MailToFileSender.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

LessSpecificReturnStatement

src/Sender/MailToFileSender.php:66:16: LessSpecificReturnStatement: The type 'array<array-key, null|string>' is more general than the declared return type 'list<non-empty-string>' for Buggregator\Trap\Sender\MailToFileSender::collectUniqueEmails (see https://psalm.dev/129)
\array_filter(
\array_merge(
\array_map($fn, $message->getBcc()),
\array_map($fn, $message->getTo()),
),
static fn(string $email): bool => false !== \filter_var($email, \FILTER_VALIDATE_EMAIL)

Check failure on line 72 in src/Sender/MailToFileSender.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

PossiblyInvalidArgument

src/Sender/MailToFileSender.php:72:17: PossiblyInvalidArgument: Parameter 1 of closure passed to function array_filter expects string, but possibly different type null|string provided (see https://psalm.dev/092)
),
);
}
}
27 changes: 22 additions & 5 deletions tests/Unit/Sender/MailToFileSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ public function testForSmtp(): void
$message = SmtpMessage::create(
protocol: [
'FROM' => ['<[email protected]>'],
'BCC' => ['<[email protected]>', '<[email protected]>'],
'BCC' => [
'<[email protected]>',
'<[email protected]>',
'', // must be ignored
],
],
headers: [
'From' => ['Some User <[email protected]>'],
'To' => ['User1 <[email protected]>'],
'To' => [
'User1 <[email protected]>',
'User3 <[email protected]>',
'User without email', // no email
],
'Subject' => ['Very important theme'],
'Content-Type' => ['text/plain'],
],
Expand All @@ -38,15 +46,24 @@ public function testForSmtp(): void
$sender = new MailToFileSender($root);
$sender->send([$frame]);

$this->assertRecipient("$root/[email protected]");
$this->assertRecipient("$root/[email protected]");
$this->assertRecipient("$root/user1[at]company.tld");
$this->assertRecipient("$root/user2[at]company.tld");
$this->assertRecipient("$root/user3[at]company.tld");
}

protected function tearDown(): void
{
foreach ($this->cleanupFolders as $folder) {
\array_map('unlink', \glob("$folder/*/*.*"));
\array_map('rmdir', \glob("$folder/*"));
\rmdir($folder);
}
}

private function assertRecipient(string $folder): void
{
self::assertDirectoryExists($folder);
$files = \glob(\str_replace('[', '[[]', "$folder/*.json"));
$files = glob("$folder/*.json");
self::assertCount(1, $files);
$arr = \json_decode(\file_get_contents($files[0]), true, \JSON_THROW_ON_ERROR);
self::assertArrayHasKey('protocol', $arr);
Expand Down

0 comments on commit cb9044d

Please sign in to comment.