49 lines
1.6 KiB
PHP
49 lines
1.6 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
|
|
use DistributingCarriers\Infrastructure\Messaging\CommandBus;
|
|
use DistributingCarriers\Infrastructure\Routing\Router;
|
|
use DistributingCarriers\Infrastructure\EventSourcing\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
|
|
$eventStore = new CassandraEventStore($session);
|
|
$commandBus = new CommandBus();
|
|
|
|
// Register Handlers
|
|
$commandBus->register(RegisterCarrierCommand::class, new RegisterCarrierHandler($eventStore));
|
|
|
|
// Router
|
|
$router = new Router();
|
|
|
|
$router->add('POST', '/register-carrier', function () use ($commandBus) {
|
|
$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 = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
|
|
|
|
$router->dispatch($method, $uri);
|