Completed
Pull Request — master (#182)
by Sergey
02:46
created

CurlHttpClient   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 219
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadCookies() 0 7 1
A execute() 0 13 1
A getErrors() 0 4 1
A init() 0 12 2
A getDefaultHttpOptions() 0 13 1
A makeHttpOptions() 0 15 2
A setOptions() 0 6 1
A cookie() 0 4 1
A cookies() 0 4 1
A initCookieJar() 0 6 1
A getCookieFilePath() 0 15 3
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api;
4
5
use seregazhuk\PinterestBot\Helpers\Cookies;
6
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
7
use seregazhuk\PinterestBot\Api\Contracts\HttpClient;
8
9
/**
10
 * Class CurlAdapter.
11
 */
12
class CurlHttpClient implements HttpClient
13
{
14
    /**
15
     * Custom CURL options for requests.
16
     *
17
     * @var array
18
     */
19
    protected $options = [
20
        CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0'
21
    ];
22
23
    /**
24
     * @var array
25
     */
26
    protected $headers = [];
27
28
    /**
29
     * Contains the curl instance.
30
     *
31
     * @var resource
32
     */
33
    protected $curl;
34
35
    /**
36
     * @var string
37
     */
38
    protected $cookieJar;
39
40
    /**
41
     * Cookies container
42
     *
43
     * @var Cookies
44
     */
45
    protected $cookies;
46
47
    public function __construct(Cookies $cookies)
48
    {
49
        $this->cookies = $cookies;
50
    }
51
52
    /**
53
     * Load cookies for specified username
54
     *
55
     * @param string $username
56
     * @return HttpClient
57
     */
58
    public function loadCookies($username = '')
59
    {
60
        $this->initCookieJar($username);
61
        $this->cookies->fill($this->cookieJar);
62
63
        return $this;
64
    }
65
66
    /**
67
     * Executes curl request.
68
     *
69
     * @param string $url
70
     * @param string $postString
71
     * @param array $headers
72
     * @return string
73
     */
74
    public function execute($url, $postString = '', array $headers = [])
75
    {
76
        $this->headers = $headers;
77
78
        $this->init($url, $postString);
79
80
        $res = curl_exec($this->curl);
81
        curl_close($this->curl);
82
83
        $this->cookies->fill($this->cookieJar);
84
85
        return $res;
86
    }
87
88
    /**
89
     * Get curl errors.
90
     *
91
     * @return string
92
     */
93
    public function getErrors()
94
    {
95
        return curl_error($this->curl);
96
    }
97
98
    /**
99
     * Initializes curl resource with options.
100
     *
101
     * @param string $url
102
     * @param string $postString
103
     * @return $this
104
     */
105
    protected function init($url, $postString)
106
    {
107
        $this->curl = curl_init($url);
108
109
        if (empty($this->cookieJar)) {
110
            $this->loadCookies();
111
        }
112
113
        curl_setopt_array($this->curl, $this->makeHttpOptions($postString));
114
115
        return $this;
116
    }
117
118
    /**
119
     * @return array
120
     */
121
    protected function getDefaultHttpOptions()
122
    {
123
        return [
124
            CURLOPT_RETURNTRANSFER => true,
125
            CURLOPT_SSL_VERIFYPEER => false,
126
            CURLOPT_FOLLOWLOCATION => true,
127
            CURLOPT_ENCODING       => 'gzip,deflate',
128
            CURLOPT_HTTPHEADER     => $this->headers,
129
            CURLOPT_REFERER        => UrlBuilder::URL_BASE,
130
            CURLOPT_COOKIEFILE     => $this->cookieJar,
131
            CURLOPT_COOKIEJAR      => $this->cookieJar,
132
        ];
133
    }
134
135
    /**
136
     * Adds necessary curl options for query.
137
     *
138
     * @param string $postString POST query string
139
     *
140
     * @return array
141
     */
142
    protected function makeHttpOptions($postString = '')
143
    {
144
        // Union custom Curl options and default.
145
        $options = array_replace(
146
            $this->options,
147
            $this->getDefaultHttpOptions()
148
        );
149
150
        if (!empty($postString)) {
151
            $options[CURLOPT_POST] = true;
152
            $options[CURLOPT_POSTFIELDS] = $postString;
153
        }
154
155
        return $options;
156
    }
157
158
    /**
159
     * Set custom Curl options to override default
160
     *
161
     * @param array $options
162
     * @return CurlHttpClient
163
     */
164
    public function setOptions(array $options)
165
    {
166
        $this->options = $options;
167
168
        return $this;
169
    }
170
171
    /**
172
     * Get a cookie value by name
173
     *
174
     * @param $name
175
     * @return mixed
176
     */
177
    public function cookie($name)
178
    {
179
        return $this->cookies->get($name);
180
    }
181
182
    /**
183
     * Get all cookies
184
     *
185
     * @return array
186
     */
187
    public function cookies()
188
    {
189
        return $this->cookies->all();
190
    }
191
192
    /**
193
     * Init cookie file for a specified username. If username is empty we use
194
     * common cookie file for all sessions. If file does not exist it will
195
     * be created in system temp directory.
196
     *
197
     * @param $username
198
     * @return $this
199
     */
200
    protected function initCookieJar($username = '')
201
    {
202
        $this->cookieJar = $this->getCookieFilePath($username);
203
204
        return $this;
205
    }
206
207
    /**
208
     * Return cookie file name by username. If username is empty we use a
209
     * random cookie name, to be sure we have different cookies
210
     * in parallel sessions.
211
     *
212
     * @param string $username
213
     * @return string
214
     */
215
    protected function getCookieFilePath($username)
216
    {
217
        if(empty($username)) {
218
            return tempnam(sys_get_temp_dir(), self::COOKIE_PREFIX);
219
        }
220
221
        $cookieName = self::COOKIE_PREFIX . $username;
222
        $cookieFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $cookieName;
223
224
        if (!file_exists($cookieFilePath)) {
225
            touch($cookieFilePath);
226
        }
227
228
        return $cookieFilePath;
229
    }
230
}