Completed
Push — master ( d8f4d5...2e0c08 )
by Sergey
46s
created

CurlHttpClient::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api;
4
5
use seregazhuk\PinterestBot\Api\Contracts\HttpClient;
6
7
/**
8
 * Class CurlAdapter.
9
 *
10
 * @property string $cookieJar
11
 * @property string $cookePath
12
 */
13
class CurlHttpClient implements HttpClient
14
{
15
    /**
16
     * Contains the curl instance.
17
     *
18
     * @var resource
19
     */
20
    private $curl;
21
22
    /**
23
     * Executes curl request.
24
     *
25
     * @param string $url
26
     * @param array $options
27
     *
28
     * @return string
29
     */
30
    public function execute($url, array $options = [])
31
    {
32
        $this->init($url)->setOptions($options);
33
        $res = curl_exec($this->curl);
34
        $this->close();
35
36
        return $res;
37
    }
38
    
39
    /**
40
     * Initializes curl resource.
41
     *
42
     * @param string $url
43
     *
44
     * @return $this
45
     */
46
    protected function init($url)
47
    {
48
        $this->curl = curl_init($url);
49
50
        return $this;
51
    }
52
53
    /**
54
     * Sets multiple options at the same time.
55
     *
56
     * @param array $options
57
     *
58
     * @return static
59
     */
60
    protected function setOptions(array $options = [])
61
    {
62
        curl_setopt_array($this->curl, $options);
63
64
        return $this;
65
    }
66
67
    /**
68
     * Get curl errors.
69
     *
70
     * @return string
71
     */
72
    public function getErrors()
73
    {
74
        return curl_error($this->curl);
75
    }
76
77
    /**
78
     * Close the curl resource.
79
     *
80
     * @return void
81
     */
82
    protected function close()
83
    {
84
        curl_close($this->curl);
85
    }
86
}
87