Completed
Push — master ( 9d0a53...ecd7bc )
by Maik
01:48
created

HttpClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 13
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 3
crap 2
1
<?php
2
3
/**
4
 * This file is part of the PHP Generics package.
5
 *
6
 * @package Generics
7
 */
8
namespace Generics\Client;
9
10
use Generics\Socket\ClientSocket;
11
use Generics\Socket\Url;
12
use Generics\Streams\HttpStream;
13
14
/**
15
 * This class implements a HttpStream as client
16
 *
17
 * @author Maik Greubel <[email protected]>
18
 */
19
class HttpClient extends ClientSocket implements HttpStream
20
{
21
    use HttpClientTrait;
22
    
23
    /**
24
     * Whether to use https instead of http
25
     *
26
     * @var boolean
27
     */
28
    private $secure;
29
30
    /**
31
     * Create a new http client
32
     *
33
     * @param Url $url
34
     *            The url for http request
35
     * @param string $proto
36
     *            The protocol to use (default = HTTP/1.1)
37
     * @param integer $timeout
38
     *            Optional timeout for request (default = 10 seconds)
39
     */
40 View Code Duplication
    public function __construct(Url $url, $proto = 'HTTP/1.1', $timeout = 10)
41
    {
42
        parent::__construct($url);
43
        
44
        $this->secure = $url->getScheme() == 'https';
45
46
        $this->setTimeout($timeout);
47
        $this->setPath($url->getPath());
48
        $this->setProtocol($proto);
49
        $this->setQueryString($url->getQueryString());
50
        $this->reset();
51
        $this->resetHeaders();
52
    }
53
54
    /**
55
     *
56
     * {@inheritdoc}
57
     * @see \Generics\Streams\HttpStream::request()
58
     */
59
    public function request(string $requestType)
60
    {
61
        if ($this->secure) {
62
            throw new HttpException("Secure connection using HTTPs is not supported!");
63
        }
64
        
65
        $this->requestImpl($requestType);
66
    }
67
}
68