Completed
Pull Request — master (#289)
by Gavin
16:24 queued 13:47
created

HttpClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 36.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 107
ccs 11
cts 30
cp 0.3667
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createUri() 0 4 1
A __construct() 0 5 1
A getResponse() 0 9 1
A getHttpClient() 0 8 2
B createRequest() 0 22 4
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\Test;
13
14
use Http\Discovery\HttpClientDiscovery;
15
use Http\Discovery\MessageFactoryDiscovery;
16
use Http\Discovery\UriFactoryDiscovery;
17
use Http\Client\HttpClient as PhpHttpClient;
18
use Psr\Http\Message\RequestInterface;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\UriInterface;
21
22
/**
23
 * Provides a very simple way to fetch HTTP responses, auto-creating a client.
24
 */
25
class HttpClient
26
{
27
    /**
28
     * HTTP client for requests to the application
29
     *
30
     * @var HttpClient
31
     */
32
    private $httpClient;
33
34
    /**
35
     * @var string
36
     */
37
    private $hostname;
38
39
    /**
40
     * @var string
41
     */
42
    private $port;
43
44
    /**
45
     * @param string $hostname Default hostname if not specified in the URL
46
     * @param string $port     Default port if not specified in the URL
47
     */
48 29
    public function __construct($hostname, $port)
49
    {
50 29
        $this->hostname = $hostname;
51 29
        $this->port = $port;
52 29
    }
53
54
    /**
55
     * Get HTTP response from your application
56
     *
57
     * @param string $uri     HTTP URI
58
     * @param array  $headers HTTP headers
59
     * @param string $method  HTTP method
60
     *
61
     * @return ResponseInterface
62
     */
63 29
    public function getResponse($uri, array $headers, $method)
64
    {
65
        // Close connections to make sure invalidation (PURGE/BAN) requests will
66
        // not interfere with content (GET) requests.
67 29
        $headers['Connection'] = 'Close';
68 29
        $request = $this->createRequest($method, $uri, $headers);
69
70
        return $this->getHttpClient()->sendRequest($request);
71
    }
72
73
    /**
74
     * Get HTTP adapter for your application
75
     *
76
     * @return PhpHttpClient
77
     */
78
    private function getHttpClient()
79
    {
80
        if ($this->httpClient === null) {
81
            $this->httpClient = HttpClientDiscovery::find();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Http\Discovery\HttpClientDiscovery::find() of type object<Http\Client\HttpClient> is incompatible with the declared type object<FOS\HttpCache\Test\HttpClient> of property $httpClient.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
82
        }
83
84
        return $this->httpClient;
85
    }
86
87
    /**
88
     * Create a request
89
     *
90
     * @param string $method
91
     * @param string $uri
92
     * @param array  $headers
93
     *
94
     * @return RequestInterface
95
     * @throws \Exception
96
     */
97 29
    private function createRequest($method, $uri, $headers)
98
    {
99 29
        $uri = $this->createUri($uri);
100
        if ($uri->getHost() === '') {
101
            // Add base URI host
102
            $uri = $uri->withHost($this->hostname);
103
        }
104
105
        if (!$uri->getPort()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uri->getPort() of type null|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
106
            $uri = $uri->withPort($this->port);
107
        }
108
109
        if ($uri->getScheme() === '') {
110
            $uri = $uri->withScheme('http');
111
        }
112
113
        return MessageFactoryDiscovery::find()->createRequest(
114
            $method,
115
            $uri,
116
            $headers
117
        );
118
    }
119
120
    /**
121
     * Create PSR-7 URI object from URI string
122
     *
123
     * @param string $uriString
124
     *
125
     * @return UriInterface
126
     */
127 29
    private function createUri($uriString)
128
    {
129 29
        return UriFactoryDiscovery::find()->createUri($uriString);
130
    }
131
}
132