Completed
Push — master ( 704539...2a5253 )
by Robbie
02:16
created

src/HttpRequestAdapter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Robbie\Psr7;
4
5
use GuzzleHttp\Psr7\ServerRequest;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Robbie\Psr7\AbstractHttpAdapter;
8
use SilverStripe\Control\HTTPRequest;
9
10
/**
11
 * @package psr7-adapters
12
 */
13
class HttpRequestAdapter extends AbstractHttpAdapter
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $serverVars;
19
20
    /**
21
     * Set up the server vars - they can be overridden if required
22
     */
23
    public function __construct()
0 ignored issues
show
__construct uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
24
    {
25
        $this->setServerVars($_SERVER);
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function toPsr7($input)
32
    {
33
        $request = new ServerRequest(
34
            $input->httpMethod(),
35
            $this->getUri($input->getURL()),
36
            $input->getHeaders(),
37
            $input->getBody(),
38
            $this->getProtocolVersion(),
39
            $this->getServerVars()
40
        );
41
42
        if (!empty($input->getVars())) {
43
            $request = $request->withQueryParams($input->getVars());
44
        }
45
46
        if (!empty($input->postVars())) {
47
            $request = $request->withParsedBody($input->postVars());
48
        }
49
50
        return $request;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function fromPsr7($input)
57
    {
58
        $adapted = new HTTPRequest(
59
            $input->getMethod(),
60
            (string) $input->getUri()->getPath(),
61
            $input->getQueryParams(),
62
            $input->getParsedBody(),
63
            (string) $input->getBody()
64
        );
65
66
        $this->importHeaders($input, $adapted);
67
68
        return $adapted;
69
    }
70
71
    /**
72
     * Get the full request URI (can be empty, but probably won't be)
73
     *
74
     * @param  string $path
75
     * @return string
76
     */
77
    public function getUri($path)
78
    {
79
        $vars = $this->getServerVars();
80
81
        $uri = '';
82
        $protocol = (isset($vars['HTTPS']) || $vars['SERVER_PORT'] === '443') ? 'https' : 'http';
83
        $uri .= $protocol . '://';
84
85
        if (!empty($vars['PHP_AUTH_USER'])) {
86
            $uri .= $vars['PHP_AUTH_USER'];
87
88
            if (!empty($vars['PHP_AUTH_PW'])) {
89
                $uri .= ':' . $vars['PHP_AUTH_PW'];
90
            }
91
92
            $uri .= '@';
93
        }
94
95
        if (!empty($vars['HTTP_HOST'])) {
96
            $uri .= $vars['HTTP_HOST'];
97
        }
98
99
        $uri .= '/' . ltrim($path, '/');
100
101
        return $uri;
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function getServerVars()
108
    {
109
        return $this->serverVars;
110
    }
111
112
    /**
113
     * @param  array $vars
114
     * @return $this
115
     */
116
    public function setServerVars(array $vars)
117
    {
118
        $this->serverVars = $vars;
119
        return $this;
120
    }
121
}
122