Completed
Push — master ( 2e65b6...009716 )
by Gabor
03:45
created

GuzzleHttpAdapter::getRequestUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
namespace WebHemi\Adapter\Http\GuzzleHttp;
13
14
use GuzzleHttp\Psr7\LazyOpenStream;
15
use GuzzleHttp\Psr7\Response;
16
use GuzzleHttp\Psr7\ServerRequest;
17
use GuzzleHttp\Psr7\Uri;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
use Psr\Http\Message\StreamInterface;
21
use WebHemi\Adapter\Http\HttpAdapterInterface;
22
23
/**
24
 * Class GuzzleHttpAdapter.
25
 */
26
class GuzzleHttpAdapter implements HttpAdapterInterface
27
{
28
    /** @var ServerRequest */
29
    private $request;
30
    /** @var ResponseInterface */
31
    private $response;
32
33
    /** @var array */
34
    private $server;
35
    /** @var array */
36
    private $get;
37
    /** @var array */
38
    private $post;
39
    /** @var array */
40
    private $cookie;
41
    /** @var array */
42
    private $files;
43
44
    /**
45
     * GuzzleHTTPAdapter constructor.
46
     *
47
     * @param array $server
48
     * @param array $get
49
     * @param array $post
50
     * @param array $cookie
51
     * @param array $files
52
     */
53 View Code Duplication
    public function __construct(array $server, array $get, array $post, array $cookie, array $files)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $this->server = $server;
56
        $this->get = $get;
57
        $this->post = $post;
58
        $this->cookie = $cookie;
59
        $this->files = $files;
60
61
        $this->initialize();
62
    }
63
64
    /**
65
     * Initialize adapter: create the ServerRequest and Response instances.
66
     */
67
    private function initialize()
68
    {
69
        $uri = new Uri('');
70
        $uri = $uri->withScheme($this->getScheme())
71
            ->withHost($this->getHost())
0 ignored issues
show
Bug introduced by
It seems like $this->getHost() targeting WebHemi\Adapter\Http\Guz...eHttpAdapter::getHost() can also be of type array<integer,string>; however, GuzzleHttp\Psr7\Uri::withHost() does only seem to accept string, 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...
72
            ->withPort($this->getServerData('SERVER_PORT', 80))
73
            ->withPath($this->getRequestUri())
74
            ->withQuery($this->getServerData('QUERY_STRING', ''));
75
76
        $serverRequest = new ServerRequest(
77
            $this->getServerData('REQUEST_METHOD', 'GET'),
78
            $uri,
79
            [],
80
            new LazyOpenStream('php://input', 'r+'),
81
            $this->getProtocol(),
82
            $this->server
83
        );
84
        $this->request = $serverRequest
85
            ->withCookieParams($this->cookie)
86
            ->withQueryParams($this->get)
87
            ->withParsedBody($this->post);
88
        // Create a Response with HTTP 102 - Processing.
89
        $this->response = new Response(102);
90
    }
91
92
    /**
93
     * Gets the specific server data, or a default value if not present.
94
     *
95
     * @param string $keyName
96
     * @param mixed  $defaultValue
97
     *
98
     * @return mixed|null
99
     */
100
    private function getServerData($keyName, $defaultValue = '')
101
    {
102
        if (isset($this->server[$keyName])) {
103
            $defaultValue = $this->server[$keyName];
104
        }
105
106
        return $defaultValue;
107
    }
108
109
    /**
110
     * Gets server scheme.
111
     *
112
     * @return string
113
     */
114
    private function getScheme()
115
    {
116
        $scheme = 'http';
117
        $https = $this->getServerData('HTTPS', 'off');
118
119
        if ($https == 'on') {
120
            $scheme = 'https';
121
        }
122
123
        return $scheme;
124
    }
125
126
    /**
127
     * Gets the server host name.
128
     *
129
     * @return string
130
     */
131
    private function getHost()
132
    {
133
134
        $host = $this->getServerData('HTTP_HOST');
135
        $name = $this->getServerData('SERVER_NAME');
136
137
        if (empty($host) && !empty($name)) {
138
            $host = $name;
139
        }
140
141
        return preg_replace('/:[0-9]+$/', '', $host);
142
    }
143
144
    /**
145
     * Gets the server request uri.
146
     *
147
     * @return string
148
     */
149
    private function getRequestUri()
150
    {
151
        $requestUri = $this->getServerData('REQUEST_URI', '/');
152
153
        return current(explode('?', $requestUri));
154
    }
155
156
    /**
157
     * Gets the server protocol.
158
     *
159
     * @return string
160
     */
161
    private function getProtocol()
162
    {
163
        $protocol = '1.1';
164
        $serverProtocol = $this->getServerData('SERVER_PROTOCOL');
165
166
        if (!empty($serverProtocol)) {
167
            $protocol = str_replace('HTTP/', '', $serverProtocol);
168
        }
169
170
        return $protocol;
171
    }
172
173
    /**
174
     * Returns the HTTP request.
175
     *
176
     * @return ServerRequestInterface
177
     */
178
    public function getRequest()
179
    {
180
        return $this->request;
181
    }
182
183
    /**
184
     * Returns the response being sent.
185
     *
186
     * @return ResponseInterface
187
     */
188
    public function getResponse()
189
    {
190
        return $this->response;
191
    }
192
}
193