HttpListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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