-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151: add Sender mail to file
- Loading branch information
Showing
11 changed files
with
307 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Sender; | ||
|
||
use Buggregator\Trap\Proto\Frame; | ||
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 | ||
*/ | ||
class MailToFileSender implements Sender | ||
{ | ||
private readonly string $path; | ||
|
||
public function __construct( | ||
string $path = 'runtime/mail', | ||
) { | ||
$this->path = \rtrim($path, '/\\'); | ||
FileSystem::mkdir($path); | ||
} | ||
|
||
public function send(iterable $frames): void | ||
{ | ||
/** @var Frame $frame */ | ||
foreach ($frames as $frame) { | ||
if (!$frame instanceof Smtp) { | ||
continue; | ||
} | ||
|
||
foreach (self::fetchDirectories($frame->message) as $dirName) { | ||
$path = $this->path . DIRECTORY_SEPARATOR . $dirName; | ||
FileSystem::mkdir($path); | ||
$filepath = \sprintf("%s/%s.json", $path, $frame->time->format('Y-m-d-H-i-s-v')); | ||
|
||
\assert(!\file_exists($filepath)); | ||
\file_put_contents($filepath, \json_encode($frame->message, \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get normalized email address for file or directory name. | ||
* | ||
* @return non-empty-string | ||
Check failure on line 50 in src/Sender/MailToFileSender.php GitHub Actions / psalm (ubuntu-latest, 8.2, locked)MoreSpecificReturnType
|
||
*/ | ||
private static function normalizeEmail(string $email): string | ||
{ | ||
return \preg_replace( | ||
Check failure on line 54 in src/Sender/MailToFileSender.php GitHub Actions / psalm (ubuntu-latest, 8.2, locked)LessSpecificReturnStatement
|
||
['/[^a-z0-9.\\- @]/i', '/\s+/'], | ||
['!', '_'], | ||
$email, | ||
); | ||
} | ||
|
||
/** | ||
* @return list<non-empty-string> | ||
Check failure on line 62 in src/Sender/MailToFileSender.php GitHub Actions / psalm (ubuntu-latest, 8.2, locked)MoreSpecificReturnType
|
||
*/ | ||
private static function fetchDirectories(Message\Smtp $message): array | ||
{ | ||
return | ||
\array_filter( | ||
Check failure on line 67 in src/Sender/MailToFileSender.php GitHub Actions / psalm (ubuntu-latest, 8.2, locked)LessSpecificReturnStatement
|
||
\array_unique( | ||
\array_map( | ||
static fn(Contact $c) => self::normalizeEmail($c->email), | ||
Check failure on line 70 in src/Sender/MailToFileSender.php GitHub Actions / psalm (ubuntu-latest, 8.2, locked)PossiblyNullArgument
|
||
\array_merge($message->getBcc(), $message->getTo()), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Support; | ||
|
||
/** | ||
* @internal | ||
* @psalm-internal Buggregator\Trap | ||
*/ | ||
final class FileSystem | ||
{ | ||
public static function mkdir(string $path, int $mode = 0777, bool $recursive = true): void | ||
{ | ||
\is_dir($path) or \mkdir($path, $mode, $recursive) or \is_dir($path) or throw new \RuntimeException( | ||
\sprintf('Directory "%s" was not created.', $path), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Tests\Unit\Sender; | ||
|
||
use Buggregator\Trap\Info; | ||
use Buggregator\Trap\Proto\Frame\Smtp as SmtpFrame; | ||
use Buggregator\Trap\Sender\MailToFileSender; | ||
use Buggregator\Trap\Traffic\Message\Smtp as SmtpMessage; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Buggregator\Trap\Sender\MailToFileSender | ||
*/ | ||
final class MailToFileSenderTest extends TestCase | ||
{ | ||
/** @var list<non-empty-string> */ | ||
private array $cleanupFolders = []; | ||
|
||
public function testForSmtp(): void | ||
{ | ||
$this->cleanupFolders[] = $root = Info::TRAP_ROOT . '/runtime/tests/mail-to-file-sender'; | ||
|
||
$message = SmtpMessage::create( | ||
protocol: [ | ||
'FROM' => ['<someusername@foo.bar>'], | ||
'BCC' => [ | ||
'<user1@company.tld>', | ||
'<user2@company.tld>', | ||
], | ||
], | ||
headers: [ | ||
'From' => ['Some User <someusername@somecompany.ru>'], | ||
'To' => [ | ||
'User1 <user1@company.tld>', | ||
'user2@company.tld', | ||
'User without email', // no email | ||
'User3 <user3@inline.com>, User4 <user4@inline.com>, user5@inline.com', | ||
], | ||
'Subject' => ['Very important theme'], | ||
'Content-Type' => ['text/plain'], | ||
], | ||
); | ||
$frame = new SmtpFrame($message); | ||
$sender = new MailToFileSender($root); | ||
$sender->send([$frame]); | ||
|
||
$this->assertRecipient("$root/user1@company.tld"); | ||
$this->assertRecipient("$root/user2@company.tld"); | ||
$this->assertRecipient("$root/user3@inline.com"); | ||
$this->assertRecipient("$root/user4@inline.com"); | ||
$this->assertRecipient("$root/user5@inline.com"); | ||
} | ||
|
||
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")); | ||
self::assertCount(1, $files); | ||
$arr = \json_decode(\file_get_contents($files[0]), true, \JSON_THROW_ON_ERROR); | ||
self::assertArrayHasKey('protocol', $arr); | ||
self::assertArrayHasKey('headers', $arr); | ||
self::assertArrayHasKey('messages', $arr); | ||
self::assertArrayHasKey('attachments', $arr); | ||
} | ||
} |
Oops, something went wrong.