Completed
Push — master ( fd8346...c1962d )
by
unknown
08:45 queued 06:48
created

CurlHttpClient::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Client;
4
5
use Symfony\Component\HttpFoundation\Response;
6
7
class CurlHttpClient implements HttpClientInterface
8
{
9
    /**
10
     * @var int
11
     */
12
    private $connectTimeout = 5;
13
14
    /**
15
     * @var int
16
     */
17
    private $timeout = 5;
18
19
    /**
20
     * @param string $url
21
     * @return string
22
     */
23 4
    public function getData($url)
24
    {
25 4
        $ch = curl_init();
26
27 4
        curl_setopt($ch, CURLOPT_HEADER, 0);
28 4
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
29 4
        curl_setopt($ch, CURLOPT_URL, $url);
30
31 4
        $data = curl_exec($ch);
32 4
        curl_close($ch);
33
34 4
        return $data;
35
    }
36
37
    /**
38
     * @param string $url
39
     * @return bool
40
     */
41 1
    public function exists($url)
42
    {
43 1
        $ch = curl_init();
44 1
        curl_setopt($ch, CURLOPT_URL, $url);
45 1
        curl_setopt($ch, CURLOPT_NOBODY, true);
46 1
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
47 1
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
48 1
        curl_setopt($ch, CURLOPT_HEADER, false);
49 1
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
50
51 1
        curl_exec($ch);
52 1
        $info = curl_getinfo($ch);
53 1
        curl_close($ch);
54
55 1
        return $info['http_code'] === Response::HTTP_OK;
56
    }
57
}
58