Skip to content

Commit b8ca69e

Browse files
wundiiawu
andauthored
chore: update classes to final and readonly for consistency, enhance container startup logic (#22)
Co-authored-by: awu <andreas.wunderwald@westpress.de>
1 parent 9f93f9b commit b8ca69e

8 files changed

Lines changed: 44 additions & 19 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"Thenativeweb\\Eventsourcingdb\\Tests\\": "tests/"
1414
},
1515
"files": [
16-
"tests/getImageVersionFromDockerfile.php"
16+
"tests/Fn/getImageVersionFromDockerfile.php"
1717
]
1818
},
1919
"authors": [

src/Bound.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enum BoundType: string
1212
case EXCLUSIVE = 'exclusive';
1313
}
1414

15-
class Bound implements JsonSerializable
15+
final readonly class Bound implements JsonSerializable
1616
{
1717
public function __construct(
1818
public string $id,

src/Container.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,42 @@ public function withPort(int $port): self
4242
return $this;
4343
}
4444

45+
/**
46+
* @throws Exception
47+
*/
4548
public function start(): void
4649
{
47-
$container =
48-
(new GenericContainer("{$this->imageName}:{$this->imageTag}"))
49-
->withExposedPorts($this->internalPort)
50-
->withCommand([
51-
'run',
52-
'--api-token', $this->apiToken,
53-
'--data-directory-temporary',
54-
'--http-enabled',
55-
'--https-enabled=false',
56-
]);
50+
$container = null;
51+
$retryCount = 5;
52+
while ($retryCount) {
53+
try {
54+
$container =
55+
(new GenericContainer("{$this->imageName}:{$this->imageTag}"))
56+
->withExposedPorts($this->internalPort)
57+
->withCommand([
58+
'run',
59+
'--api-token', $this->apiToken,
60+
'--data-directory-temporary',
61+
'--http-enabled',
62+
'--https-enabled=false',
63+
]);
64+
65+
} catch (Exception $exception) {
66+
--$retryCount;
67+
68+
$exceptionMessage = $exception->getMessage();
69+
70+
sleep(6);
71+
}
72+
73+
if ($container instanceof GenericContainer) {
74+
break;
75+
}
76+
}
77+
78+
if (!$container instanceof GenericContainer) {
79+
exit($exceptionMessage ?? 'Failed to create container');
80+
}
5781

5882
$this->container = $container->start();
5983

src/EventType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use JsonSerializable;
88

9-
class EventType implements JsonSerializable
9+
final readonly class EventType implements JsonSerializable
1010
{
1111
public function __construct(
1212
public string $eventType,

src/ObserveEventsOptions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enum ObserveIfEventIsMissing: string
1212
case WAIT_FOR_EVENT = 'wait-for-event';
1313
}
1414

15-
class ObserveFromLatestEvent implements JsonSerializable
15+
final readonly class ObserveFromLatestEvent implements JsonSerializable
1616
{
1717
public function __construct(
1818
public string $subject,
@@ -31,7 +31,7 @@ public function jsonSerialize(): array
3131
}
3232
}
3333

34-
class ObserveEventsOptions implements JsonSerializable
34+
final readonly class ObserveEventsOptions implements JsonSerializable
3535
{
3636
public function __construct(
3737
public bool $recursive,

src/ReadEventsOptions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ enum ReadIfEventIsMissing: string
1818
case READ_EVERYTHING = 'read-everything';
1919
}
2020

21-
class ReadFromLatestEvent implements JsonSerializable
21+
final readonly class ReadFromLatestEvent implements JsonSerializable
2222
{
2323
public function __construct(
2424
public string $subject,
@@ -37,7 +37,7 @@ public function jsonSerialize(): array
3737
}
3838
}
3939

40-
class ReadEventsOptions implements JsonSerializable
40+
final readonly class ReadEventsOptions implements JsonSerializable
4141
{
4242
public function __construct(
4343
public bool $recursive = false,

tests/ClientTestTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Thenativeweb\Eventsourcingdb\Client;
88
use Thenativeweb\Eventsourcingdb\Container;
9+
use function Thenativeweb\Eventsourcingdb\Tests\Fn\getImageVersionFromDockerfile;
910

1011
trait ClientTestTrait
1112
{

tests/getImageVersionFromDockerfile.php renamed to tests/Fn/getImageVersionFromDockerfile.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace Thenativeweb\Eventsourcingdb\Tests;
5+
namespace Thenativeweb\Eventsourcingdb\Tests\Fn;
66

77
use RuntimeException;
88

99
function getImageVersionFromDockerfile(): string
1010
{
11-
$dockerfile = __DIR__ . '/../docker/Dockerfile';
11+
$dockerfile = __DIR__ . '/../../docker/Dockerfile';
1212

1313
if (!file_exists($dockerfile)) {
1414
throw new RuntimeException('Dockerfile not found at ' . $dockerfile);

0 commit comments

Comments
 (0)