1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the AMFWebServicesClientBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Amine Fattouch <http://github.com/fattouchsquall> |
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 AMF\WebServicesClientBundle\Rest\EventListener; |
13
|
|
|
|
14
|
|
|
use AMF\WebServicesClientBundle\Rest\Event\GetResponseEvent; |
15
|
|
|
use AMF\WebServicesClientBundle\Rest\Component\Response; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Listener for http rest requests. |
19
|
|
|
* |
20
|
|
|
* @author Mohamed Amine Fattouch <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class HttpListener |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $options; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var resource |
31
|
|
|
*/ |
32
|
|
|
protected $curlHandle; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The constructor class. |
36
|
|
|
* |
37
|
|
|
* @param array $options Options for curl (default empty). |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $options = []) |
40
|
|
|
{ |
41
|
|
|
$this->options = $options; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Sends a GET request to the API. |
46
|
|
|
* |
47
|
|
|
* @param GetResponseEvent $getResponseEvent The event for sharing rest response and request. |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function onGetRequest(GetResponseEvent $getResponseEvent) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this->curlHandle = curl_init(); |
52
|
|
|
curl_setopt($this->curlHandle, CURLOPT_HTTPGET, true); |
53
|
|
|
|
54
|
|
|
$this->execute($getResponseEvent); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sends a POST request to the API. |
59
|
|
|
* |
60
|
|
|
* @param GetResponseEvent $getResponseEvent The event for sharing rest response and request. |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function onPostRequest(GetResponseEvent $getResponseEvent) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->curlHandle = curl_init(); |
65
|
|
|
curl_setopt($this->curlHandle, CURLOPT_POST, true); |
66
|
|
|
|
67
|
|
|
$this->execute($getResponseEvent); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Sends a PUT request to the API. |
72
|
|
|
* |
73
|
|
|
* @param GetResponseEvent $getResponseEvent The event for sharing rest response and request. |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function onPutRequest(GetResponseEvent $getResponseEvent) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$this->curlHandle = curl_init(); |
78
|
|
|
curl_setopt($this->curlHandle, CURLOPT_CUSTOMREQUEST, 'PUT'); |
79
|
|
|
|
80
|
|
|
$this->execute($getResponseEvent); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sends a DELETE request to the API. |
85
|
|
|
* |
86
|
|
|
* @param GetResponseEvent $getResponseEvent The event for sharing rest response and request. |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function onDeleteRequest(GetResponseEvent $getResponseEvent) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$this->curlHandle = curl_init(); |
91
|
|
|
curl_setopt($this->curlHandle, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
92
|
|
|
|
93
|
|
|
$this->execute($getResponseEvent); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Executes a curl request to the given url. |
98
|
|
|
* |
99
|
|
|
* @param GetResponseEvent $event The event for sharing rest response and request. |
100
|
|
|
*/ |
101
|
|
|
protected function execute(GetResponseEvent $event) |
102
|
|
|
{ |
103
|
|
|
$request = $event->getRequest(); |
104
|
|
|
$server = $request->getServer(); |
105
|
|
|
$headers = $request->buildHttpHeaders(); |
106
|
|
|
|
107
|
|
|
$requestString = $server->get('REQUEST_STRING'); |
108
|
|
|
if (!empty($requestString) && strlen($requestString) > 0) { |
109
|
|
|
curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $requestString); |
110
|
|
|
} |
111
|
|
|
curl_setopt($this->curlHandle, CURLOPT_URL, $server->get('REQUEST_URI')); |
112
|
|
|
curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, $this->options['return_transfer']); |
113
|
|
|
curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, $this->options['timeout']); |
114
|
|
|
curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, $this->options['ssl_verifypeer']); |
115
|
|
|
curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYHOST, $this->options['ssl_verifyhost']); |
116
|
|
|
curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
117
|
|
|
|
118
|
|
|
$result = curl_exec($this->curlHandle); |
119
|
|
|
$info = curl_getinfo($this->curlHandle); |
120
|
|
|
|
121
|
|
|
curl_close($this->curlHandle); |
122
|
|
|
unset($this->curlHandle); |
123
|
|
|
|
124
|
|
|
$response = Response::create($result, $info['http_code']); |
125
|
|
|
|
126
|
|
|
$event->setResponse($response); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.