Completed
Push — master ( 9cb483...15ad7c )
by Maik
03:24
created

HttpClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 3
crap 1
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 12 View Code Duplication
    public function __construct(Url $url, $proto = 'HTTP/1.1', $timeout = 10)
41
    {
42 12
        parent::__construct($url);
43
        
44 12
        $this->secure = $url->getScheme() == 'https';
45
46 12
        $this->setTimeout($timeout);
47 12
        $this->setPath($url->getPath());
48 12
        $this->setProtocol($proto);
49 12
        $this->setQueryString($url->getQueryString());
50 12
        $this->reset();
51 12
        $this->resetHeaders();
52 12
    }
53
54
    /**
55
     *
56
     * {@inheritdoc}
57
     * @see \Generics\Streams\HttpStream::request()
58
     */
59 12
    public function request(string $requestType)
60
    {
61 12
        if ($this->secure) {
62 1
            throw new HttpException("Secure connection using HTTPs is not supported!");
63
        }
64
        
65 11
        $this->requestImpl($requestType);
66 9
    }
67
}
68