Passed
Push — master ( 0d72e8...44be60 )
by Gabor
04:58
created

ServiceAdapter::getHost()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 0
crap 3
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 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
declare(strict_types = 1);
13
14
namespace WebHemi\Http\ServiceAdapter\GuzzleHttp;
15
16
use GuzzleHttp\Psr7\LazyOpenStream;
17
use GuzzleHttp\Psr7\Uri;
18
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
19
use WebHemi\Http\ResponseInterface;
20
use WebHemi\Http\ServerRequestInterface;
21
use WebHemi\Http\ServiceInterface;
22
23
/**
24
 * Class ServiceAdapter.
25
 */
26
class ServiceAdapter implements ServiceInterface
27
{
28
    /** @var ServerRequest */
29
    private $request;
30
    /** @var Response */
31
    private $response;
32
    /** @var array */
33
    private $get;
34
    /** @var array */
35
    private $post;
36
    /** @var array */
37
    private $server;
38
    /** @var array */
39
    private $cookie;
40
    /** @var array */
41
    private $files;
42
43
    /**
44
     * ServiceAdapter constructor.
45
     *
46
     * @param EnvironmentInterface $environmentManager
47
     */
48 9
    public function __construct(EnvironmentInterface $environmentManager)
49
    {
50 9
        $this->get = $environmentManager->getEnvironmentData('GET');
51 9
        $this->post = $environmentManager->getEnvironmentData('POST');
52 9
        $this->server = $environmentManager->getEnvironmentData('SERVER');
53 9
        $this->cookie = $environmentManager->getEnvironmentData('COOKIE');
54 9
        $this->files = $environmentManager->getEnvironmentData('FILES');
55
56 9
        $this->initialize();
57 9
    }
58
59
    /**
60
     * Initialize adapter: create the ServerRequest and Response instances.
61
     *
62
     * @return void
63
     */
64 9
    private function initialize() : void
65
    {
66 9
        $uri = new Uri('');
67 9
        $uri = $uri->withScheme($this->getScheme())
68 9
            ->withHost($this->getHost())
69 9
            ->withPort($this->getServerData('SERVER_PORT', '80'))
70 9
            ->withPath($this->getRequestUri())
71 9
            ->withQuery($this->getServerData('QUERY_STRING', ''));
72
73 9
        $serverRequest = new ServerRequest(
74 9
            $this->getServerData('REQUEST_METHOD', 'GET'),
75
            $uri,
76 9
            [],
77 9
            new LazyOpenStream('php://input', 'r+'),
78 9
            $this->getProtocol(),
79 9
            $this->server
80
        );
81 9
        $this->request = $serverRequest
82 9
            ->withCookieParams($this->cookie)
83 9
            ->withQueryParams($this->get)
84 9
            ->withParsedBody($this->post);
85
        // Create a Response with HTTP 102 - Processing.
86 9
        $this->response = new Response(Response::STATUS_PROCESSING);
87 9
    }
88
89
    /**
90
     * Gets the specific server data, or a default value if not present.
91
     *
92
     * @param string $keyName
93
     * @param string $defaultValue
94
     * @return string
95
     */
96 9
    private function getServerData(string $keyName, string $defaultValue = '') : string
97
    {
98 9
        if (isset($this->server[$keyName])) {
99 9
            $defaultValue = $this->server[$keyName];
100
        }
101
102 9
        return (string) $defaultValue;
103
    }
104
105
    /**
106
     * Gets server scheme.
107
     *
108
     * @return string
109
     */
110 9
    private function getScheme() : string
111
    {
112 9
        $https = $this->getServerData('HTTPS', 'off');
113
114 9
        return $https == 'on' ? 'https' : 'http';
115
    }
116
117
    /**
118
     * Gets the server host name.
119
     *
120
     * @return string
121
     */
122 9
    private function getHost() : string
123
    {
124 9
        $host = $this->getServerData('HTTP_HOST');
125 9
        $name = $this->getServerData('SERVER_NAME');
126
127 9
        if (empty($host) && !empty($name)) {
128 1
            $host = $name;
129
        }
130
131 9
        return (string) preg_replace('/:[0-9]+$/', '', $host);
132
    }
133
134
    /**
135
     * Gets the server request uri.
136
     *
137
     * @return string
138
     */
139 9
    private function getRequestUri() : string
140
    {
141 9
        $requestUri = $this->getServerData('REQUEST_URI', '/');
142
143 9
        return (string) current(explode('?', $requestUri));
144
    }
145
146
    /**
147
     * Gets the server protocol.
148
     *
149
     * @return string
150
     */
151 9
    private function getProtocol() : string
152
    {
153 9
        $protocol = '1.1';
154 9
        $serverProtocol = $this->getServerData('SERVER_PROTOCOL');
155
156 9
        if (!empty($serverProtocol)) {
157 1
            $protocol = str_replace('HTTP/', '', $serverProtocol);
158
        }
159
160 9
        return (string) $protocol;
161
    }
162
163
    /**
164
     * Returns the HTTP request.
165
     *
166
     * @return ServerRequestInterface
167
     */
168 6
    public function getRequest() : ServerRequestInterface
169
    {
170 6
        return $this->request;
171
    }
172
173
    /**
174
     * Returns the response being sent.
175
     *
176
     * @return ResponseInterface
177
     */
178 6
    public function getResponse() : ResponseInterface
179
    {
180 6
        return $this->response;
181
    }
182
}
183