1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: sdaoudi |
5
|
|
|
* Date: 11/03/18 |
6
|
|
|
* Time: 15:35 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace SfForward\Controller; |
10
|
|
|
|
11
|
|
|
use GuzzleHttp\Client; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use SfForward\Util\ParamsListService; |
14
|
|
|
use SfForward\Util\RequestMethodMapping; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
18
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
19
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class SfForwardController |
23
|
|
|
*/ |
24
|
|
|
class SfForwardController extends Controller |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @Route("/sfForwardFront/{paramsList}", name="sf_forward_front") |
28
|
|
|
* |
29
|
|
|
* @param Request $request |
30
|
|
|
* @param string $paramsList |
31
|
|
|
* |
32
|
|
|
* @return Response |
33
|
|
|
*/ |
34
|
|
|
public function sfForwardFront(Request $request, $paramsList) |
35
|
|
|
{ |
36
|
|
|
$params = new ParamsListService($paramsList); |
37
|
|
|
$guzzleServiceName = sprintf('eight_points_guzzle.client.%s', $params->getServiceName()); |
38
|
|
|
$method = strtolower($request->getMethod()); |
39
|
|
|
|
40
|
|
|
if (RequestMethodMapping::isValidMethod($method) && $this->has($guzzleServiceName)) { |
41
|
|
|
/** @var Client $client */ |
42
|
|
|
$client = $this->get($guzzleServiceName); |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** @var ResponseInterface $guzzleResponse */ |
46
|
|
|
$guzzleResponse = $client->{$method}( |
47
|
|
|
$params->getRouteId(), |
48
|
|
|
['query' => $request->{RequestMethodMapping::$methodsMapping[$method]}->all()] |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$response = new Response($guzzleResponse->getBody()); |
52
|
|
|
$response->setStatusCode($guzzleResponse->getStatusCode()); |
53
|
|
|
$response->setProtocolVersion($guzzleResponse->getProtocolVersion()); |
54
|
|
|
|
55
|
|
|
return $response; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
throw new NotFoundHttpException(sprintf('Service "%s" not found', $params->getServiceName())); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |