Skip to content

Commit

Permalink
chore: add FileSystem helper; cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 15, 2025
1 parent c93be24 commit bd68525
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Proto/Frame/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(

public static function fromString(string $payload, \DateTimeImmutable $time): static
{
$payload = \json_decode($payload, true, \JSON_THROW_ON_ERROR);
$payload = \json_decode($payload, true, 64, \JSON_THROW_ON_ERROR);

$request = new ServerRequest(
$payload['method'] ?? 'GET',
Expand Down
2 changes: 1 addition & 1 deletion src/Proto/Frame/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function __construct(

public static function fromString(string $payload, \DateTimeImmutable $time): static
{
$payload = \json_decode($payload, true, 64, \JSON_THROW_ON_ERROR);

Check failure on line 30 in src/Proto/Frame/Smtp.php

View workflow job for this annotation

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

MixedAssignment

src/Proto/Frame/Smtp.php:30:9: MixedAssignment: Unable to determine the type that $payload is being assigned to (see https://psalm.dev/032)
/** @var TArrayData $payload */
$payload = \json_decode($payload, true, \JSON_THROW_ON_ERROR);
$message = Message\Smtp::fromArray($payload);

return new self($message, $time);
Expand Down
5 changes: 2 additions & 3 deletions src/Sender/BodyToFileSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Buggregator\Trap\Proto\Frame;
use Buggregator\Trap\Sender;
use Buggregator\Trap\Support\FileSystem;
use Buggregator\Trap\Support\StreamHelper;
use Nyholm\Psr7\Stream;

Expand All @@ -23,9 +24,7 @@ public function __construct(
string $path = 'runtime/body',
) {
$this->path = \rtrim($path, '/\\');
if (!\is_dir($path) && !\mkdir($path, 0o777, true) && !\is_dir($path)) {
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $path));
}
FileSystem::mkdir($path);
}

public function send(iterable $frames): void
Expand Down
5 changes: 2 additions & 3 deletions src/Sender/EventsToFileSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Buggregator\Trap\Proto\Frame;
use Buggregator\Trap\Sender;
use Buggregator\Trap\Support\FileSystem;

/**
* Store event groups to files.
Expand All @@ -21,9 +22,7 @@ public function __construct(
string $path = 'runtime',
) {
$this->path = \rtrim($path, '/\\');
if (!\is_dir($path) && !\mkdir($path, 0o777, true) && !\is_dir($path)) {
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $path));
}
FileSystem::mkdir($path);
}

public function send(iterable $frames): void
Expand Down
14 changes: 5 additions & 9 deletions src/Sender/MailToFileSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Buggregator\Trap\Proto\Frame;
use Buggregator\Trap\Proto\Frame\Smtp;
use Buggregator\Trap\Sender;
use Buggregator\Trap\Support\FileSystem;

/**
* @internal
Expand All @@ -17,12 +18,9 @@ class MailToFileSender implements Sender

public function __construct(
string $path = 'runtime/mail',
)
{
) {
$this->path = \rtrim($path, '/\\');
if (!\is_dir($path) && !\mkdir($path, 0o777, true) && !\is_dir($path)) {
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $path));
}
FileSystem::mkdir($path);
}

public function send(iterable $frames): void
Expand All @@ -34,14 +32,12 @@ public function send(iterable $frames): void
}

foreach ($frame->message->getBcc() as $bcc) {
if (null === $bcc->email) {
if ($bcc->email === null) {
continue;
}

$path = $this->path . DIRECTORY_SEPARATOR . $bcc->email;
if (!\is_dir($path) && !\mkdir($path, 0o777, true) && !\is_dir($path)) {
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $path));
}
FileSystem::mkdir($path);
$filepath = \sprintf("%s/%s.json", $path, \date('Y-m-d-H-i-s-v'));
\assert(!\file_exists($filepath));
\file_put_contents($filepath, \json_encode($frame->message, \JSON_THROW_ON_ERROR));
Expand Down
19 changes: 19 additions & 0 deletions src/Support/FileSystem.php
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),
);
}
}
18 changes: 9 additions & 9 deletions tests/Unit/Sender/MailToFileSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ final class MailToFileSenderTest extends TestCase
{
private array $cleanupFolders = [];

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

public function testForSmtp(): void
{
$this->cleanupFolders[] = $root = \sys_get_temp_dir() . DIRECTORY_SEPARATOR . \uniqid('trap_mail_');
Expand All @@ -48,11 +40,19 @@ public function testForSmtp(): void
$this->assertRecipient("$root/[email protected]");
}

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

private function assertRecipient(string $folder): void
{
self::assertTrue(\file_exists($folder));
self::assertTrue(\is_dir($folder));
$files = glob("$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 bd68525

Please sign in to comment.