Completed
Push — master ( c28f31...fd8346 )
by
unknown
09:46
created

CurlHttpClient   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 0
dl 0
loc 51
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 13 1
A exists() 0 16 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
    public function getData($url)
24
    {
25
        $ch = curl_init();
26
27
        curl_setopt($ch, CURLOPT_HEADER, 0);
28
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
29
        curl_setopt($ch, CURLOPT_URL, $url);
30
31
        $data = curl_exec($ch);
32
        curl_close($ch);
33
34
        return $data;
35
    }
36
37
    /**
38
     * @param string $url
39
     * @return bool
40
     */
41
    public function exists($url)
42
    {
43
        $ch = curl_init();
44
        curl_setopt($ch, CURLOPT_URL, $url);
45
        curl_setopt($ch, CURLOPT_NOBODY, true);
46
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
47
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
48
        curl_setopt($ch, CURLOPT_HEADER, false);
49
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
50
51
        curl_exec($ch);
52
        $info = curl_getinfo($ch);
53
        curl_close($ch);
54
55
        return $info['http_code'] === Response::HTTP_OK;
56
    }
57
}
58