feat(infrastructure): Enhance router and API endpoint with proper error handling
This commit is contained in:
@@ -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']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Cassandra {
|
||||
class Timestamp
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
// Mock Cassandra Session
|
||||
class MockCassandraSession
|
||||
{
|
||||
public $executedStatements = [];
|
||||
|
||||
public function prepare($cql)
|
||||
{
|
||||
return new MockStatement($cql);
|
||||
}
|
||||
|
||||
public function execute($statement, $options)
|
||||
{
|
||||
$this->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";
|
||||
}
|
||||
Reference in New Issue
Block a user