Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
CurlWrapper
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 8
272
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 setOpt
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
72
 exec
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 close
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getInfo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getErrNo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getError
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCommand
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace 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 */
8class 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}