|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Http request helper, to make http client in one place. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\ProxyApiBundle\Helper; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\ProxyApiBundle\Listener\ProxyExceptionListener; |
|
9
|
|
|
use GuzzleHttp\Client as httpClient; |
|
10
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
11
|
|
|
use GuzzleHttp\Promise\RejectedPromise; |
|
12
|
|
|
use GuzzleHttp\Psr7\Response as ClientResponse; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Request manager and service definition start up |
|
17
|
|
|
* Build by compiler to be used in ProxyManager |
|
18
|
|
|
* |
|
19
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
20
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
21
|
|
|
* @link http://swisscom.ch |
|
22
|
|
|
*/ |
|
23
|
|
|
class HttpHelper |
|
24
|
|
|
{ |
|
25
|
|
|
protected $options = []; |
|
26
|
|
|
protected $requestUri = ''; |
|
27
|
|
|
protected $requestMethod = 'GET'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* HttpHelper for Guzzle operations. |
|
31
|
|
|
* |
|
32
|
|
|
* @param httpClient $httpClient Sf Request information service |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct( |
|
35
|
|
|
httpClient $httpClient |
|
36
|
|
|
) { |
|
37
|
|
|
$this->httpClient = $httpClient; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $uri Remote server Base url |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function setBaseUri($uri) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->options['base_uri'] = $uri; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $uri Remote server Base url |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setRequestUri($uri) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->requestUri = $uri; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $method GET | POST | ... |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setMethod($method) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->requestMethod = $method; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Client request header |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $name key field |
|
71
|
|
|
* @param string $value assignation |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
View Code Duplication |
public function addHeader($name, $value) |
|
75
|
|
|
{ |
|
76
|
|
|
if (!array_key_exists('headers', $this->options)) { |
|
77
|
|
|
$this->options['headers'] = []; |
|
78
|
|
|
} |
|
79
|
|
|
$this->options['headers'] = [$name => $value]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Client request query params |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $name key field |
|
86
|
|
|
* @param string $value assignation |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
View Code Duplication |
public function addQueryParams($name, $value) |
|
90
|
|
|
{ |
|
91
|
|
|
if (!array_key_exists('query', $this->options)) { |
|
92
|
|
|
$this->options['query'] = []; |
|
93
|
|
|
} |
|
94
|
|
|
$this->options['query'] = [$name => $value]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Run request against server |
|
99
|
|
|
* |
|
100
|
|
|
* @return Response |
|
101
|
|
|
*/ |
|
102
|
|
|
public function execute() |
|
103
|
|
|
{ |
|
104
|
|
|
try { |
|
105
|
|
|
/** @var ClientResponse $httpResponse */ |
|
106
|
|
|
$httpResponse = $this->httpClient->request( |
|
107
|
|
|
$this->requestMethod, |
|
108
|
|
|
$this->requestUri, |
|
109
|
|
|
$this->options |
|
110
|
|
|
); |
|
111
|
|
|
} catch (ClientException $e) { |
|
112
|
|
|
$status = $e->getResponse()->getStatusCode(); |
|
113
|
|
|
throw new ProxyExceptionListener($status, 'Could not execute request, rejected by server.'); |
|
114
|
|
|
} catch (\Exception $e) { |
|
115
|
|
|
throw new ProxyExceptionListener(400, 'Could not execute request, failure.'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$response = new Response(); |
|
119
|
|
|
$response->headers->set('Content-Type', 'application/json'); |
|
120
|
|
|
$headers = $httpResponse->getHeaders(); |
|
121
|
|
|
$httpResponse->getStatusCode(); |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
// Make response with correct content headers |
|
124
|
|
|
if (array_key_exists('Content-Type', $headers)) { |
|
125
|
|
|
$response->headers->set('Content-Type', $headers['Content-Type']); |
|
126
|
|
|
} |
|
127
|
|
|
$response->setContent($httpResponse->getBody()); |
|
128
|
|
|
$response->setStatusCode($httpResponse->getStatusCode()); |
|
129
|
|
|
|
|
130
|
|
|
return $response; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: