Skip to content

Commit bd68525

Browse files
committed
chore: add FileSystem helper; cleanup
1 parent c93be24 commit bd68525

File tree

7 files changed

+39
-26
lines changed

7 files changed

+39
-26
lines changed

src/Proto/Frame/Http.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(
3737

3838
public static function fromString(string $payload, \DateTimeImmutable $time): static
3939
{
40-
$payload = \json_decode($payload, true, \JSON_THROW_ON_ERROR);
40+
$payload = \json_decode($payload, true, 64, \JSON_THROW_ON_ERROR);
4141

4242
$request = new ServerRequest(
4343
$payload['method'] ?? 'GET',

src/Proto/Frame/Smtp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public function __construct(
2727

2828
public static function fromString(string $payload, \DateTimeImmutable $time): static
2929
{
30+
$payload = \json_decode($payload, true, 64, \JSON_THROW_ON_ERROR);
3031
/** @var TArrayData $payload */
31-
$payload = \json_decode($payload, true, \JSON_THROW_ON_ERROR);
3232
$message = Message\Smtp::fromArray($payload);
3333

3434
return new self($message, $time);

src/Sender/BodyToFileSender.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Buggregator\Trap\Proto\Frame;
88
use Buggregator\Trap\Sender;
9+
use Buggregator\Trap\Support\FileSystem;
910
use Buggregator\Trap\Support\StreamHelper;
1011
use Nyholm\Psr7\Stream;
1112

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

3130
public function send(iterable $frames): void

src/Sender/EventsToFileSender.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Buggregator\Trap\Proto\Frame;
88
use Buggregator\Trap\Sender;
9+
use Buggregator\Trap\Support\FileSystem;
910

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

2928
public function send(iterable $frames): void

src/Sender/MailToFileSender.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Buggregator\Trap\Proto\Frame;
88
use Buggregator\Trap\Proto\Frame\Smtp;
99
use Buggregator\Trap\Sender;
10+
use Buggregator\Trap\Support\FileSystem;
1011

1112
/**
1213
* @internal
@@ -17,12 +18,9 @@ class MailToFileSender implements Sender
1718

1819
public function __construct(
1920
string $path = 'runtime/mail',
20-
)
21-
{
21+
) {
2222
$this->path = \rtrim($path, '/\\');
23-
if (!\is_dir($path) && !\mkdir($path, 0o777, true) && !\is_dir($path)) {
24-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $path));
25-
}
23+
FileSystem::mkdir($path);
2624
}
2725

2826
public function send(iterable $frames): void
@@ -34,14 +32,12 @@ public function send(iterable $frames): void
3432
}
3533

3634
foreach ($frame->message->getBcc() as $bcc) {
37-
if (null === $bcc->email) {
35+
if ($bcc->email === null) {
3836
continue;
3937
}
4038

4139
$path = $this->path . DIRECTORY_SEPARATOR . $bcc->email;
42-
if (!\is_dir($path) && !\mkdir($path, 0o777, true) && !\is_dir($path)) {
43-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $path));
44-
}
40+
FileSystem::mkdir($path);
4541
$filepath = \sprintf("%s/%s.json", $path, \date('Y-m-d-H-i-s-v'));
4642
\assert(!\file_exists($filepath));
4743
\file_put_contents($filepath, \json_encode($frame->message, \JSON_THROW_ON_ERROR));

src/Support/FileSystem.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Buggregator\Trap\Support;
6+
7+
/**
8+
* @internal
9+
* @psalm-internal Buggregator\Trap
10+
*/
11+
final class FileSystem
12+
{
13+
public static function mkdir(string $path, int $mode = 0777, bool $recursive = true): void
14+
{
15+
\is_dir($path) or \mkdir($path, $mode, $recursive) or \is_dir($path) or throw new \RuntimeException(
16+
\sprintf('Directory "%s" was not created.', $path),
17+
);
18+
}
19+
}

tests/Unit/Sender/MailToFileSenderTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ final class MailToFileSenderTest extends TestCase
1616
{
1717
private array $cleanupFolders = [];
1818

19-
protected function tearDown(): void
20-
{
21-
foreach ($this->cleanupFolders as $folder) {
22-
\array_map('unlink', glob("$folder/*.*"));
23-
\rmdir($folder);
24-
}
25-
}
26-
2719
public function testForSmtp(): void
2820
{
2921
$this->cleanupFolders[] = $root = \sys_get_temp_dir() . DIRECTORY_SEPARATOR . \uniqid('trap_mail_');
@@ -48,11 +40,19 @@ public function testForSmtp(): void
4840
$this->assertRecipient("$root/user2@company.tld");
4941
}
5042

43+
protected function tearDown(): void
44+
{
45+
foreach ($this->cleanupFolders as $folder) {
46+
\array_map('unlink', \glob("$folder/*.*"));
47+
\rmdir($folder);
48+
}
49+
}
50+
5151
private function assertRecipient(string $folder): void
5252
{
5353
self::assertTrue(\file_exists($folder));
5454
self::assertTrue(\is_dir($folder));
55-
$files = glob("$folder/*.json");
55+
$files = \glob("$folder/*.json");
5656
self::assertCount(1, $files);
5757
$arr = \json_decode(\file_get_contents($files[0]), true, \JSON_THROW_ON_ERROR);
5858
self::assertArrayHasKey('protocol', $arr);

0 commit comments

Comments
 (0)