Created
January 13, 2014 21:10
-
-
Save brianium/8408227 to your computer and use it in GitHub Desktop.
Swamp of POX with Silex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$app = new \Silex\Application(); | |
$doOrder = function(\SimpleXMLElement $order) { | |
$orderService = new OrderService(); | |
$orderService->setBeerId((string) $order['beer_id']); | |
$creditCard = CreditCardFactory::fromXml($order->CreditCard); | |
$orderService->setCreditCard($creditCard); | |
$order = $orderService->createOrder(); | |
return XmlMapper::map($order); | |
}; | |
$getOrderStatus = function(\SimpleXMLElement $status) { | |
$statusFetcher = new StatusFetcher(); | |
$statusFetcher->setOrderId((string) $status['order_id']); | |
$status = $statusFetcher->fetch(); | |
return XmlMapper::map($status); | |
}; | |
$app->post('/orderService', function(Request $request) use ($doOrder, $getOrderStatus) { | |
$body = simplexml_load_string($request->getContent()); | |
$name = $body->getName(); | |
switch ($name) { | |
case: 'OrderRequest': | |
return $doOrder($body); | |
case: 'OrderStatusRequest': | |
return $getOrderStatus($body); | |
default: | |
return ErrorHandler::handle(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A request for placing an order might look like this:
POST /orderService HTTP/1.1
And its response might look like this:
A request for checking the status of an order might look like this:
POST /orderService HTTP/1.1
With a response that might look like this: