SymfonyHttpBridge   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 9
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convertSymfonyRequest() 0 23 4
A convertInfuseResponse() 0 13 2
1
<?php
2
3
namespace Infuse;
4
5
use Symfony\Component\HttpFoundation\Cookie;
6
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
7
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
8
9
/**
10
 * Converts Symfony request/response objects to
11
 * Infuse request/response objects, and vice-versa.
12
 */
13
class SymfonyHttpBridge
14
{
15
    /**
16
     * Converts a Symfony request object into an Infuse request object.
17
     *
18
     * @param SymfonyRequest $request
19
     *
20
     * @return Request
21
     */
22
    public function convertSymfonyRequest(SymfonyRequest $request)
23
    {
24
        $session = $request->getSession();
25
        if ($session) {
26
            $session = $session->all();
27
        } else {
28
            $session = [];
29
        }
30
31
        // decode request parameters
32
        $parameters = $request->request->all();
33
        if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH']) && 'json' == $request->getContentType()) {
34
            $parameters = json_decode($request->getContent(), true);
35
        }
36
37
        // NOTE: This is not entirely accurate to use $_FILES, however,
38
        // because Symfony converts $_FILES into an UploadedFile object
39
        // and Infuse expects the raw array, it makes compatibility difficult.
40
        $req = new Request($request->query->all(), $parameters, $request->cookies->all(), $_FILES, $request->server->all(), $session);
41
        $req->setParams($request->attributes->all());
42
43
        return $req;
44
    }
45
46
    /**
47
     * Converts an Infuse response object into a Symfony response object.
48
     *
49
     * @param Response $res
50
     *
51
     * @return SymfonyResponse
52
     */
53
    public function convertInfuseResponse(Response $res)
54
    {
55
        $response = new SymfonyResponse($res->getBody(), $res->getCode(), $res->headers());
0 ignored issues
show
Bug introduced by
It seems like $res->headers() targeting Infuse\Response::headers() can also be of type null or string; however, Symfony\Component\HttpFo...Response::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
56
57
        // transfer cookies
58
        foreach ($res->cookies() as $name => $params) {
59
            list($value, $expire, $path, $domain, $secure, $httponly) = $params;
60
            $cookie = new Cookie($name, $value, $expire, $path, $domain, $secure, $httponly);
61
            $response->headers->setCookie($cookie);
62
        }
63
64
        return $response;
65
    }
66
}
67