Skip to content

Commit

Permalink
Fixed formatting, logging, and env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilJimenez21 authored and nekufa committed Oct 1, 2024
1 parent bac7349 commit 744a5f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
36 changes: 22 additions & 14 deletions src/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function __construct(
$this->registerVerbs();
}

private function generateId(): string {
private function generateId(): string
{
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$charactersLength = strlen($characters);
Expand All @@ -68,21 +69,23 @@ private function ping(): array

private function info(): array
{
$endpoints = [];

foreach ($this->endpoints as $endpoint) {
$endpoints[] = [
'name' => $endpoint->getName(),
'subject' => $endpoint->getSubject(),
'queue_group' => $endpoint->getQueueGroup(),
];
}

return [
'type' => 'io.nats.micro.v1.info_response',
'name' => $this->name,
'id' => $this->id,
'version' => $this->version,
'description' => $this->description,
'endpoints' => array_reduce($this->endpoints, function ($carry, $endpoint) {
$carry[] = [
'name' => $endpoint->getName(),
'subject' => $endpoint->getSubject(),
'queue_group' => $endpoint->getQueueGroup(),
];

return $carry;
}, []),
'endpoints' => $endpoints
];
}

Expand Down Expand Up @@ -213,14 +216,19 @@ private function controlSubject(string $verb, string $name, string $id): string
return "\$SRV.$verb.$name.$id";
}

public function run() : void
public function run(): void
{
print_r("$this->name is ready to accept connections\n");
while(true) {
$this->client
->logger
->info("$this->name is ready to accept connections\n");

while (true) {
try {
$this->client->process();
} catch (\Exception $e) {
print_r("$this->name encountered an error:\n" . $e->getMessage() . "\n");
$this->client
->logger
->error("$this->name encountered an error:\n" . $e->getMessage() . "\n");
}
}
}
Expand Down
20 changes: 8 additions & 12 deletions src/Service/ServiceEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ class ServiceEndpoint

public function __construct(
private readonly Service $service,
private readonly string $name,
private readonly string $subject,
private $endpointHandler,
private readonly string $queue_group = 'q'
)
{
private readonly string $name,
private readonly string $subject,
private $endpointHandler,
private readonly string $queue_group = 'q'
) {
$this->subscription = $this->service->client->subscribeQueue(
$this->subject,
$this->queue_group,
Expand All @@ -40,15 +39,10 @@ function (Payload $message) {
$response = "";

switch ($this->endpointHandler) {
case is_string($this->endpointHandler):
case is_subclass_of($this->endpointHandler, EndpointHandler::class):
// Instantiate the endpointHandler
$handler = new $this->endpointHandler();

// Check to make sure that the class implements ServiceEndpoint
if (!($handler instanceof EndpointHandler)) {
throw new \LogicException("Class must implement EndpointHandler");
}

$response = $handler->handle($message);
break;
case is_callable($this->endpointHandler):
Expand All @@ -59,6 +53,8 @@ function (Payload $message) {
case $this->endpointHandler instanceof EndpointHandler:
$response = $this->endpointHandler->handle($message);
break;
default:
throw new \LogicException("The provided endpoint handler is not a supported type.");
}

// Add to the total processing time
Expand Down
8 changes: 2 additions & 6 deletions tests/Functional/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@

class ServiceTest extends FunctionalTestCase
{
private bool $tested = false;

private function createTestService(): Service
{
/** @var Client $client */
$client = $this->createClient([
'host' => 'hermes.internal'
]);
$client = $this->createClient();

/** @var Service $service */
$service = $client->service(
Expand Down Expand Up @@ -68,7 +64,7 @@ function (Payload $payload) {
$this->assertTrue($response['success']);
}

public function testServiceRequestReplyClass()
public function testServiceRequestReplyInstance()
{
$service = $this->createTestService();

Expand Down

0 comments on commit 744a5f9

Please sign in to comment.