diff --git a/src/Service/Service.php b/src/Service/Service.php index 54bdf9b..0022a38 100644 --- a/src/Service/Service.php +++ b/src/Service/Service.php @@ -42,7 +42,8 @@ public function __construct( $this->registerVerbs(); } - private function generateId(): string { + private function generateId(): string + { $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $charactersLength = strlen($characters); @@ -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 ]; } @@ -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"); } } } diff --git a/src/Service/ServiceEndpoint.php b/src/Service/ServiceEndpoint.php index ce0820b..5fbcecf 100644 --- a/src/Service/ServiceEndpoint.php +++ b/src/Service/ServiceEndpoint.php @@ -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, @@ -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): @@ -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 diff --git a/tests/Functional/ServiceTest.php b/tests/Functional/ServiceTest.php index be5742a..d8e8683 100644 --- a/tests/Functional/ServiceTest.php +++ b/tests/Functional/ServiceTest.php @@ -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( @@ -68,7 +64,7 @@ function (Payload $payload) { $this->assertTrue($response['success']); } - public function testServiceRequestReplyClass() + public function testServiceRequestReplyInstance() { $service = $this->createTestService();