Passed
Push — master ( a92d5c...8cd674 )
by Alexandre
02:54
created

Curl::getCurlResponseFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * php-guard/curl <https://github.com/php-guard/curl>
4
 * Copyright (C) ${YEAR} by Alexandre Le Borgne <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace PhpGuard\Curl;
21
22
class Curl
23
{
24
    private $requestModifierPipeline;
25
    private $curlResponseFactory;
26
    private $curlRequestFactory;
27
28
    public function __construct(?string $host = null)
29
    {
30
        $this->requestModifierPipeline = new RequestModifierPipeline();
31
        $this->curlResponseFactory = new CurlResponseFactory();
32
        $this->curlRequestFactory = new CurlRequestFactory($this, $host);
33
34
        $this->requestModifierPipeline
35
            ->pipe(new FileRequestModifier())
36
            ->pipe(new PlainTextRequestModifier());
37
    }
38
39
    /**
40
     * @param CurlRequest[] $requests
41
     * @param array $options Définit les options pour le gestionnaire multiple cURL
42
     *
43
     * @return CurlResponse[]
44
     *
45
     * @throws CurlError
46
     */
47 1
    public function multi(array $requests, array $options = [])
48
    {
49 1
        $mh = curl_multi_init();
50
51 1
        foreach ($options as $key => $value) {
52
            curl_multi_setopt($mh, $key, $value);
53
        }
54
55 1
        $chs = [];
56 1
        foreach ($requests as $request) {
57 1
            $ch = $this->prepare($request);
58 1
            $chs[] = $ch;
59 1
            curl_multi_add_handle($mh, $ch);
60
        }
61
62 1
        $active = null;
63
64
        do {
65 1
            $code = curl_multi_exec($mh, $active);
66 1
            curl_multi_select($mh);
67 1
        } while ($active > 0);
68
69 1
        $responses = [];
70
71
        try {
72 1
            if ($code > CURLM_OK) {
73
                throw new CurlError(curl_multi_strerror($code), $code);
74
            }
75
76 1
            foreach ($chs as $ch) {
77 1
                $output = curl_multi_getcontent($ch);
78
79 1
                if (false === $output) {
80
                    $message = curl_error($ch);
81
                    $code = curl_errno($ch);
82
83
                    throw new CurlError($message, $code);
84
                }
85
86 1
                $responses[] = $this->curlResponseFactory->create($output, curl_getinfo($ch));
87
            }
88
        } catch (CurlError $curlError) {
89
            throw $curlError;
90 1
        } finally {
91 1
            foreach ($chs as $ch) {
92 1
                curl_multi_remove_handle($mh, $ch);
93
            }
94 1
            curl_multi_close($mh);
95
        }
96
97 1
        return $responses;
98
    }
99
100
    /**
101
     * @param CurlRequest $request
102
     *
103
     * @return resource
104
     */
105 13
    protected function prepare(CurlRequest $request)
106
    {
107 13
        $request = $this->requestModifierPipeline->process($request);
108
109
        // create curl resource
110 13
        $ch = curl_init();
111
112 13
        curl_setopt($ch, CURLOPT_URL, $request->getUrl());
113 13
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getData());
114 13
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod());
115 13
        curl_setopt($ch, CURLOPT_HTTPHEADER, $request->getHeaders()->toHttp());
116
117 13
        foreach ($request->getCurlOptions()->all() as $key => $value) {
118 13
            curl_setopt($ch, $key, $value);
119
        }
120
121 13
        return $ch;
122
    }
123
124
    /**
125
     * @param string $url
126
     * @param null|array|string $query
127
     * @param array $headers
128
     *
129
     * @return CurlRequest
130
     */
131 5
    public function get(string $url, $query = null, array $headers = [])
132
    {
133 5
        return $this->curlRequestFactory->create('GET', $url, null, $query, $headers);
134
    }
135
136 6
    public function post(string $url, $data = null, $query = null, array $headers = [])
137
    {
138 6
        return $this->curlRequestFactory->create('POST', $url, $data, $query, $headers);
139
    }
140
141 1
    public function put(string $url, $data = null, $query = null, array $headers = [])
142
    {
143 1
        return $this->curlRequestFactory->create('PUT', $url, $data, $query, $headers);
144
    }
145
146 1
    public function patch(string $url, $data = null, $query = null, array $headers = [])
147
    {
148 1
        return $this->curlRequestFactory->create('PATCH', $url, $data, $query, $headers);
149
    }
150
151 1
    public function delete(string $url, $data = null, $query = null, array $headers = [])
152
    {
153 1
        return $this->curlRequestFactory->create('DELETE', $url, $data, $query, $headers);
154
    }
155
156
    /**
157
     * @param CurlRequest $request
158
     *
159
     * @return CurlResponse
160
     *
161
     * @throws CurlError
162
     */
163 12
    public function execute(CurlRequest $request): CurlResponse
164
    {
165 12
        $ch = $this->prepare($request);
166
167
        // $output contains the output string
168 12
        $output = curl_exec($ch);
169
170 12
        if (false === $output) {
171
            $message = curl_error($ch);
172
            $code = curl_errno($ch);
173
174
            // close curl resource to free up system resources
175
            curl_close($ch);
176
            throw new CurlError($message, $code);
177
        }
178
179 12
        $info = curl_getinfo($ch);
180 12
        curl_close($ch);
181
182 12
        return $this->curlResponseFactory->create($output, $info);
183
    }
184
185
    /**
186
     * @return RequestModifierPipeline
187
     */
188
    public function getRequestModifierPipeline(): RequestModifierPipeline
189
    {
190
        return $this->requestModifierPipeline;
191
    }
192
193
    /**
194
     * @return CurlRequestFactory
195
     */
196
    public function getCurlRequestFactory(): CurlRequestFactory
197
    {
198
        return $this->curlRequestFactory;
199
    }
200
201
    /**
202
     * @return CurlResponseFactory
203
     */
204
    public function getCurlResponseFactory(): CurlResponseFactory
205
    {
206
        return $this->curlResponseFactory;
207
    }
208
}
209