Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.45% |
21 / 22 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
| CurlWrapper | |
95.45% |
21 / 22 |
|
87.50% |
7 / 8 |
16 | |
0.00% |
0 / 1 |
| init | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| setOpt | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
8 | |||
| exec | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| close | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getInfo | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getErrNo | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getError | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCommand | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Ably\Utils; |
| 3 | |
| 4 | /** |
| 5 | * A wrapper class around cURL functions to allow mocking. |
| 6 | * Also generates commands pastable to terminal for easier debugging. |
| 7 | */ |
| 8 | class CurlWrapper { |
| 9 | protected $commands = []; |
| 10 | |
| 11 | public function init( $url = null ) { |
| 12 | $handle = curl_init( $url ); |
| 13 | $this->commands[(int) $handle] = [ |
| 14 | 'prefix' => 'curl', |
| 15 | 'command' => ' ', |
| 16 | 'url' => $url ? : '', |
| 17 | ]; |
| 18 | |
| 19 | return $handle; |
| 20 | } |
| 21 | |
| 22 | public function setOpt( $handle, $option, $value ) { |
| 23 | if ( $option == CURLOPT_URL ) $this->commands[(int) $handle]['url'] = $value; |
| 24 | else if ( $option == CURLOPT_POST && $value ) $this->commands[(int) $handle]['command'] .= '-X POST '; |
| 25 | else if ( $option == CURLOPT_CUSTOMREQUEST ) $this->commands[(int) $handle]['command'] .= '-X ' . $value . ' '; |
| 26 | else if ( $option == CURLOPT_POSTFIELDS ) $this->commands[(int) $handle]['command'] .= '--data "'. str_replace( '"', '\"', $value ) .'" '; |
| 27 | else if ( $option == CURLOPT_HTTPHEADER ) { |
| 28 | foreach($value as $header) { |
| 29 | $this->commands[(int) $handle]['command'] .= '-H "' . str_replace( '"', '\"', $header ).'" '; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | return curl_setopt( $handle, $option, $value ); |
| 34 | } |
| 35 | |
| 36 | public function exec( $handle ) { |
| 37 | return curl_exec( $handle ); |
| 38 | } |
| 39 | |
| 40 | public function close( $handle ) { |
| 41 | unset( $this->commands[(int) $handle] ); |
| 42 | |
| 43 | return curl_close( $handle ); |
| 44 | } |
| 45 | |
| 46 | public function getInfo( $handle ) { |
| 47 | return curl_getinfo( $handle ); |
| 48 | } |
| 49 | |
| 50 | public function getErrNo( $handle ) { |
| 51 | return curl_errno( $handle ); |
| 52 | } |
| 53 | |
| 54 | public function getError( $handle ) { |
| 55 | return curl_error( $handle ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Retrieve a command pastable to terminal for a handle |
| 60 | */ |
| 61 | public function getCommand( $handle ) { |
| 62 | return $this->commands[(int) $handle]['prefix'] . $this->commands[(int) $handle]['command'] . $this->commands[(int) $handle]['url']; |
| 63 | } |
| 64 | } |