Completed
Pull Request — master (#60)
by Sergey
04:54 queued 01:12
created

CurlAdapter::getInfo()   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 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api;
4
5
use seregazhuk\PinterestBot\Contracts\HttpInterface;
6
7
/**
8
 * Class CurlAdapter
9
 *
10
 * @package seregazhuk\PinterestBot
11
 * @property string $cookieJar
12
 * @property string $cookePath
13
 */
14
class CurlAdapter implements HttpInterface
15
{
16
    /**
17
     * Contains the curl instance
18
     *
19
     * @var resource
20
     */
21
    private $curl;
22
23
    /**
24
     * Initializes curl resource
25
     *
26
     * @access public
27
     * @param string $url
28
     *
29
     * @return $this
30
     */
31
    protected function init($url)
32
    {
33
        $this->curl = curl_init($url);
34
35
        return $this;
36
    }
37
38
39
    /**
40
     * Sets multiple options at the same time
41
     *
42
     * @access public
43
     * @param  array $options
44
     * @return static
45
     */
46
    protected function setOptions(array $options = [])
47
    {
48
        curl_setopt_array($this->curl, $options);
49
50
        return $this;
51
    }
52
53
54
    /**
55
     * Check if the curl request ended up with errors
56
     *
57
     * @access public
58
     * @return boolean
59
     */
60
    public function hasErrors()
61
    {
62
        return curl_errno($this->curl) ? true : false;
63
    }
64
65
    /**
66
     * Get curl errors
67
     *
68
     * @access public
69
     * @return string
70
     */
71
    public function getErrors()
72
    {
73
        return curl_error($this->curl);
74
    }
75
76
    /**
77
     * Close the curl resource
78
     *
79
     * @access public
80
     * @return void
81
     */
82
    protected function close()
83
    {
84
        curl_close($this->curl);
85
    }
86
87
    /**
88
     * Executes curl request
89
     *
90
     * @param string $url
91
     * @param array $options
92
     * @return string
93
     */
94
    public function execute($url, array $options = [])
95
    {
96
        $this->init($url)->setOptions($options);
97
        $res = curl_exec($this->curl);
98
        $this->close();
99
100
        return $res;
101
    }
102
}
103