|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SmartboxSkeletonBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
8
|
|
|
use Smartbox\Integration\FrameworkBundle\Core\Messages\Context; |
|
9
|
|
|
use SmartboxSkeletonBundle\Entity\Result; |
|
10
|
|
|
|
|
11
|
|
|
class ApiController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
public function apiAction(Request $request, $methodName) |
|
14
|
|
|
{ |
|
15
|
|
|
if ('' == $methodName) { |
|
16
|
|
|
return $this->render('SmartboxSkeletonBundle:Default:index.html.twig'); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
if ('POST' == $request->getMethod()) {//assumed always async for demo |
|
20
|
|
|
$serializer = $this->get('jms_serializer'); |
|
21
|
|
|
$content = $request->getContent(); |
|
22
|
|
|
$data = null; |
|
|
|
|
|
|
23
|
|
|
switch ($methodName) { |
|
24
|
|
|
case 'asyncping': |
|
25
|
|
|
case 'ping': |
|
26
|
|
|
$data = $serializer->deserialize($content, 'SmartboxSkeletonBundle\Entity\PingMessage', 'json'); |
|
27
|
|
|
break; |
|
28
|
|
|
default: |
|
29
|
|
|
return new Response('{"status":"failed"}', Response::HTTP_METHOD_NOT_ALLOWED, array('Content-Type' => 'application/json')); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$responseMessage = $this->send($methodName, $data, true); |
|
33
|
|
|
$responseContext = $responseMessage->getContext(); |
|
34
|
|
|
$transactionId = $responseContext->get('transaction_id'); |
|
35
|
|
|
$code = $responseMessage->getBody()->getCode(); |
|
36
|
|
|
$json = $serializer->serialize($responseMessage->getBody(), 'json'); |
|
37
|
|
|
$response = new Response($json, $code, array('Content-Type' => 'application/json')); |
|
38
|
|
|
$response->headers->set('transactionId', $transactionId); |
|
39
|
|
|
|
|
40
|
|
|
return $response; |
|
41
|
|
|
} elseif ('GET' == $request->getMethod()) {//assumed always sync for demo |
|
42
|
|
|
$data = new Result(); //No data |
|
43
|
|
|
$responseMessage = $this->send($methodName, $data, false); |
|
44
|
|
|
$responseContext = $responseMessage->getContext(); |
|
45
|
|
|
$transactionId = $responseContext->get('transaction_id'); |
|
46
|
|
|
$serializer = $this->get('jms_serializer'); |
|
47
|
|
|
$json = $serializer->serialize($responseMessage->getBody(), 'json'); |
|
48
|
|
|
$response = new Response($json, 200, array('Content-Type' => 'application/json')); |
|
49
|
|
|
$response->headers->set('transactionId', $transactionId); |
|
50
|
|
|
|
|
51
|
|
|
return $response; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return new Response('{"status":"failed"}', Response::HTTP_METHOD_NOT_ALLOWED, array('Content-Type' => 'application/json')); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function send($methodName, $data, $async) |
|
58
|
|
|
{ |
|
59
|
|
|
$requestHandler = $this->get('smartbox_skeleton_request_handler'); |
|
60
|
|
|
$context = new Context([ |
|
61
|
|
|
Context::FLOWS_VERSION => '0', |
|
62
|
|
|
Context::TRANSACTION_ID => uniqid('', true), |
|
63
|
|
|
Context::ORIGINAL_FROM => 'api', |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
$responseMessage = $requestHandler->handleCall( |
|
67
|
|
|
'skeleton', |
|
68
|
|
|
'v0', |
|
69
|
|
|
$methodName, |
|
70
|
|
|
$data, |
|
71
|
|
|
[], |
|
72
|
|
|
$context, |
|
73
|
|
|
$async |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
return $responseMessage; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|