diff --git a/src/Proto/Frame/Http.php b/src/Proto/Frame/Http.php index 8aff074..5f5f1c3 100644 --- a/src/Proto/Frame/Http.php +++ b/src/Proto/Frame/Http.php @@ -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', diff --git a/src/Proto/Frame/Smtp.php b/src/Proto/Frame/Smtp.php index c91c286..cf0b113 100644 --- a/src/Proto/Frame/Smtp.php +++ b/src/Proto/Frame/Smtp.php @@ -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); /** @var TArrayData $payload */ - $payload = \json_decode($payload, true, \JSON_THROW_ON_ERROR); $message = Message\Smtp::fromArray($payload); return new self($message, $time); diff --git a/src/Sender/BodyToFileSender.php b/src/Sender/BodyToFileSender.php index d02a905..f1962f8 100644 --- a/src/Sender/BodyToFileSender.php +++ b/src/Sender/BodyToFileSender.php @@ -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; @@ -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 diff --git a/src/Sender/EventsToFileSender.php b/src/Sender/EventsToFileSender.php index b99daa2..a1d104a 100644 --- a/src/Sender/EventsToFileSender.php +++ b/src/Sender/EventsToFileSender.php @@ -6,6 +6,7 @@ use Buggregator\Trap\Proto\Frame; use Buggregator\Trap\Sender; +use Buggregator\Trap\Support\FileSystem; /** * Store event groups to files. @@ -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 diff --git a/src/Sender/MailToFileSender.php b/src/Sender/MailToFileSender.php index b3f25ff..a9e6187 100644 --- a/src/Sender/MailToFileSender.php +++ b/src/Sender/MailToFileSender.php @@ -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 @@ -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 @@ -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)); diff --git a/src/Support/FileSystem.php b/src/Support/FileSystem.php new file mode 100644 index 0000000..81e6c35 --- /dev/null +++ b/src/Support/FileSystem.php @@ -0,0 +1,19 @@ +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_'); @@ -48,11 +40,19 @@ public function testForSmtp(): void $this->assertRecipient("$root/user2@company.tld"); } + 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);