Curl::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Netgen\HtmlPdfApi\HttpClient;
4
5
use Netgen\HtmlPdfApi\HttpClientInterface;
6
7
8
class Curl implements HttpClientInterface {
9
10
    /**
11
     * Base url of the api
12
     *
13
     * @var string
14
     */
15
    protected $host;
16
17
    /**
18
     * Security token
19
     *
20
     * @var string
21
     */
22
    protected $token;
23
24
    /**
25
     * Constructor
26
     *
27
     * @param string $host  Base url of the api
28
     * @param string $token Security token
29
     */
30
    public function __construct($host, $token)
31
    {
32
        $this->host = $host;
33
        $this->token = $token;
34
    }
35
36
    /**
37
     * Initiates curl session for POST method
38
     *
39
     * @param string $url   Relative url for the request
40
     * @param array $params Parameters for the request
41
     * @return resource
42
     */
43
    private function initiatePost($url, $params)
44
    {
45
        $ch = curl_init($this->host.'/'.$url);
46
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authentication: Token ". $this->token));
47
        curl_setopt($ch, CURLOPT_POST, 1);
48
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
49
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
50
        return $ch;
51
    }
52
53
    /**
54
     * Initiates curl session for GET method
55
     *
56
     * @param string $url   Relative url for the request
57
     * @param array $params Parameters for the request
58
     * @return resource
59
     */
60
    private function initiateGet($url, $params)
61
    {;
62
        if (!empty($params))
63
        {
64
            $parameter=implode(array_values($params));
65
            $url .= '/'.$parameter;
66
        }
67
        $ch = curl_init($this->host.'/'.$url);
68
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authentication: Token ". $this->token));
69
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
70
71
        return $ch;
72
    }
73
74
    /**
75
     * Initiates curl session for DELETE method
76
     *
77
     * @param string $url   Relative url for the request
78
     * @param array $params Parameters for the request
79
     * @return resource
80
     */
81
    private function initiateDelete($url, $params)
82
    {
83
84
        if (!empty($params))
85
        {
86
            $parameter=implode(array_values($params));
87
            $url .= '/'.$parameter;
88
        }
89
        $ch = curl_init($this->host.'/'.$url);
90
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authentication: Token ". $this->token));
91
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
92
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
93
94
        return $ch;
95
    }
96
97
    /**
98
     * Initiate curl session for POST upload
99
     *
100
     * @param string $url   Relative url for the request
101
     * @param array $params Parameters for the request
102
     * @return resource
103
     */
104
    private function initiateUpload($url, $params)
105
    {
106
        $ch = curl_init($this->host.'/'.$url);
107
108
        if (!empty($params['file']))
109
        {
110
            $params['file'] .= ";filename=".basename($params['file']);
111
        }
112
113
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
114
            "Authentication: Token ". $this->token
115
        ));
116
        curl_setopt($ch, CURLOPT_POST, 1);
117
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
118
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
119
120
        return $ch;
121
    }
122
123
    /**
124
     * Sends the request via curl
125
     *
126
     * @param string $url       Relative url for the request
127
     * @param array $params     Parameters for the request
128
     * @param string $method    Request method
129
     * @return string
130
     */
131
    public function sendRequest($url, $params, $method)
132
    {
133
        if($method=='POST')
134
        {
135
            if (!empty($params['file']))
136
                $ch = $this->initiateUpload($url, $params);
137
            else
138
                $ch = $this->initiatePost($url, $params);
139
        }
140
        else if($method=='GET')
141
        {
142
            $ch = $this->initiateGet($url, $params);
143
        }
144
        else if($method=='DELETE')
145
        {
146
            $ch = $this->initiateDelete($url, $params);
147
        }
148
        else{
149
            throw new \Exception("Method ".$method." not supported!");
150
        }
151
152
        if (! $ret = curl_exec($ch))
153
        {
154
            trigger_error(curl_error($ch));
155
        }
156
157
        curl_close($ch);
158
        return $ret;
159
    }
160
161
}