1 | <?php |
||
2 | /** |
||
3 | * @author Tharanga Kothalawala <[email protected]> |
||
4 | * @date 30-12-2018 |
||
5 | */ |
||
6 | |||
7 | namespace TSK\SSO\Http; |
||
8 | |||
9 | /** |
||
10 | * @codeCoverageIgnore |
||
11 | * @internal |
||
12 | * @package TSK\SSO\Storage |
||
13 | * |
||
14 | * This can send GET & POST requests. @todo: Add Guzzle |
||
15 | */ |
||
16 | class CurlRequest |
||
17 | { |
||
18 | /** |
||
19 | * @var resource |
||
20 | */ |
||
21 | private $curl; |
||
22 | |||
23 | public function __construct() |
||
24 | { |
||
25 | $this->curl = curl_init(); |
||
0 ignored issues
–
show
|
|||
26 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); |
||
27 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false); |
||
28 | curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); |
||
29 | curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 120); |
||
30 | curl_setopt($this->curl, CURLOPT_TIMEOUT, 120); |
||
31 | } |
||
32 | |||
33 | public function __destruct() |
||
34 | { |
||
35 | if (!empty($this->curl)) { |
||
36 | curl_close($this->curl); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * makes a GET request to a given endpoint |
||
42 | * |
||
43 | * @param string $url external url |
||
44 | * @param array $headers http headers if any [optional] |
||
45 | * @return string |
||
46 | */ |
||
47 | public function get($url, array $headers = array()) |
||
48 | { |
||
49 | curl_setopt($this->curl, CURLOPT_POST, false); |
||
50 | |||
51 | return $this->request($url, $headers); |
||
0 ignored issues
–
show
|
|||
52 | } |
||
53 | |||
54 | /** |
||
55 | * makes a POST request to a given endpoint |
||
56 | * |
||
57 | * @param string $url external url |
||
58 | * @param array $data [optional] data to post |
||
59 | * @param array $headers [optional] http headers if any |
||
60 | * @return string |
||
61 | */ |
||
62 | public function post($url, array $data = array(), array $headers = array()) |
||
63 | { |
||
64 | if (!empty($data)) { |
||
65 | curl_setopt($this->curl, CURLOPT_POST, count($data)); |
||
66 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data); |
||
67 | } |
||
68 | |||
69 | return $this->request($url, $headers); |
||
0 ignored issues
–
show
|
|||
70 | } |
||
71 | |||
72 | /** |
||
73 | * makes a POST request to a given endpoint using URL encoded data |
||
74 | * |
||
75 | * @param string $url external url |
||
76 | * @param string $rawData [optional] data to post |
||
77 | * @param array $headers [optional] http headers if any |
||
78 | * @return string |
||
79 | */ |
||
80 | public function postUrlEncoded($url, $urlEncodedData = null, array $headers = array()) |
||
81 | { |
||
82 | if (!empty($urlEncodedData)) { |
||
83 | curl_setopt($this->curl, CURLOPT_POST, strlen($urlEncodedData)); |
||
84 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $urlEncodedData); |
||
85 | } |
||
86 | |||
87 | $headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
||
88 | |||
89 | return $this->request($url, $headers); |
||
0 ignored issues
–
show
|
|||
90 | } |
||
91 | |||
92 | /** |
||
93 | * makes a DELETE request to a given endpoint using Basic Authentication as per RFC-2617 |
||
94 | * @see https://www.ietf.org/rfc/rfc2617.txt |
||
95 | * |
||
96 | * @param string $url external url |
||
97 | * @param string $basicAuthData this should be in format username:password |
||
98 | * @param array $headers http headers if any [optional] |
||
99 | * @return string |
||
100 | */ |
||
101 | public function deleteWithBasicAuth($url, $basicAuthData, array $headers = array()) |
||
102 | { |
||
103 | curl_setopt($this->curl, CURLOPT_POST, false); |
||
104 | curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
||
105 | curl_setopt($this->curl, CURLOPT_USERPWD, $basicAuthData); |
||
106 | |||
107 | return $this->request($url, $headers); |
||
0 ignored issues
–
show
|
|||
108 | } |
||
109 | |||
110 | private function request($url, array $headers) |
||
111 | { |
||
112 | curl_setopt($this->curl, CURLOPT_URL, $url); |
||
113 | curl_setopt($this->curl, CURLOPT_REFERER, $url); |
||
114 | curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
||
115 | |||
116 | if (!empty($headers)) { |
||
117 | curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers); |
||
118 | } |
||
119 | |||
120 | return curl_exec($this->curl); |
||
121 | } |
||
122 | } |
||
123 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.