Completed
Push — master ( 26c37b...25c8ee )
by DAOUDI
02:29
created

SfForwardController::sfForwardFront()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 2
nop 2
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
}