From 5326f40b2b9afc3619d127f000fc655ab4c56b86 Mon Sep 17 00:00:00 2001 From: "m.jalmoudy" Date: Tue, 2 Dec 2025 15:00:35 +0330 Subject: [PATCH] feat(infrastructure): Enhance router and API endpoint with proper error handling --- src/Infrastructure/Router.php | 5 +-- src/index.php | 37 +++++++++++--------- tests/demo.php | 66 ----------------------------------- 3 files changed, 24 insertions(+), 84 deletions(-) delete mode 100644 tests/demo.php diff --git a/src/Infrastructure/Router.php b/src/Infrastructure/Router.php index 88cfdcd..7b45e2c 100644 --- a/src/Infrastructure/Router.php +++ b/src/Infrastructure/Router.php @@ -19,11 +19,12 @@ class Router { foreach ($this->routes as $route) { if ($route['method'] === $method && $route['path'] === $uri) { - return call_user_func($route['handler']); + return \call_user_func($route['handler']); } } http_response_code(404); - echo "Not Found"; + header('Content-Type: application/json'); + echo json_encode(['error' => 'Not Found']); } } diff --git a/src/index.php b/src/index.php index 33300b3..27d029b 100644 --- a/src/index.php +++ b/src/index.php @@ -8,17 +8,15 @@ use DistributingCarriers\Infrastructure\CassandraEventStore; use DistributingCarriers\Application\Commands\RegisterCarrierCommand; use DistributingCarriers\Application\Handlers\RegisterCarrierHandler; +// Initialize Cassandra Connection +$cluster = Cassandra::cluster() + ->withContactPoints(getenv('CASSANDRA_HOST') ?: 'cassandra') + ->withPort((int)(getenv('CASSANDRA_PORT') ?: 9042)) + ->build(); + +$session = $cluster->connect('event_store'); + // Initialize Infrastructure -// Note: In a real app, you'd configure the Cassandra session here. -// For this demo, we'll mock it or assume it's passed in. -// Since we don't have a running Cassandra instance we can connect to easily without extension, -// we will assume the $session is created. -// $cluster = Cassandra::cluster()->build(); -// $session = $cluster->connect('distributing_carriers'); - -// Mocking session for demonstration if extension is missing, or just placeholder. -$session = null; // Placeholder - $eventStore = new CassandraEventStore($session); $commandBus = new CommandBus(); @@ -29,15 +27,22 @@ $commandBus->register(RegisterCarrierCommand::class, new RegisterCarrierHandler( $router = new Router(); $router->add('POST', '/register-carrier', function () use ($commandBus) { - // In a real app, parse body. - // For demo, we'll just create a dummy command. - $command = new RegisterCarrierCommand("Test Carrier", "test@example.com"); + $input = json_decode(file_get_contents('php://input'), true); + + if (!isset($input['name']) || !isset($input['email'])) { + http_response_code(400); + echo json_encode(['error' => 'Missing required fields: name, email']); + return; + } + + $command = new RegisterCarrierCommand($input['name'], $input['email']); $commandBus->dispatch($command); + + http_response_code(201); + echo json_encode(['message' => 'Carrier registered successfully']); }); - $method = $_SERVER['REQUEST_METHOD'] ?? 'GET'; -$uri = $_SERVER['REQUEST_URI'] ?? '/'; - +$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH); $router->dispatch($method, $uri); diff --git a/tests/demo.php b/tests/demo.php deleted file mode 100644 index 6fb4257..0000000 --- a/tests/demo.php +++ /dev/null @@ -1,66 +0,0 @@ -executedStatements[] = [ - 'statement' => $statement->cql, - 'arguments' => $options['arguments'] - ]; - echo "Executed CQL: " . $statement->cql . "\n"; - return []; - } - } - - class MockStatement - { - public $cql; - public function __construct($cql) - { - $this->cql = $cql; - } - } - - require_once __DIR__ . '/../src/Infrastructure/EventStore.php'; - require_once __DIR__ . '/../src/Infrastructure/CassandraEventStore.php'; - require_once __DIR__ . '/../src/Infrastructure/CommandBus.php'; - require_once __DIR__ . '/../src/Domain/AggregateRoot.php'; - require_once __DIR__ . '/../src/Domain/Events/CarrierRegistered.php'; - require_once __DIR__ . '/../src/Domain/Carrier.php'; - require_once __DIR__ . '/../src/Application/Commands/RegisterCarrierCommand.php'; - require_once __DIR__ . '/../src/Application/Handlers/RegisterCarrierHandler.php'; - - use DistributingCarriers\Infrastructure\CommandBus; - use DistributingCarriers\Infrastructure\CassandraEventStore; - use DistributingCarriers\Application\Commands\RegisterCarrierCommand; - use DistributingCarriers\Application\Handlers\RegisterCarrierHandler; - - echo "Starting Demo...\n"; - - $session = new MockCassandraSession(); - $eventStore = new CassandraEventStore($session); - $commandBus = new CommandBus(); - - $commandBus->register(RegisterCarrierCommand::class, new RegisterCarrierHandler($eventStore)); - - $command = new RegisterCarrierCommand("Demo Carrier", "demo@example.com"); - $commandBus->dispatch($command); - - echo "Demo Completed.\n"; -}