Completed
Pull Request — master (#182)
by Sergey
06:24 queued 03:23
created

CurlHttpClient::makeHttpOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
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
     * Initializes curl resource with options.
90
     *
91
     * @param string $url
92
     * @param string $postString
93
     * @return $this
94
     */
95
    protected function init($url, $postString)
96
    {
97
        $this->curl = curl_init($url);
98
99
        if (empty($this->cookieJar)) {
100
            $this->loadCookies();
101
        }
102
103
        curl_setopt_array($this->curl, $this->makeHttpOptions($postString));
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return array
110
     */
111
    protected function getDefaultHttpOptions()
112
    {
113
        return [
114
            CURLOPT_RETURNTRANSFER => true,
115
            CURLOPT_SSL_VERIFYPEER => false,
116
            CURLOPT_FOLLOWLOCATION => true,
117
            CURLOPT_ENCODING       => 'gzip,deflate',
118
            CURLOPT_HTTPHEADER     => $this->headers,
119
            CURLOPT_REFERER        => UrlBuilder::URL_BASE,
120
            CURLOPT_COOKIEFILE     => $this->cookieJar,
121
            CURLOPT_COOKIEJAR      => $this->cookieJar,
122
        ];
123
    }
124
125
    /**
126
     * Adds necessary curl options for query.
127
     *
128
     * @param string $postString POST query string
129
     *
130
     * @return array
131
     */
132
    protected function makeHttpOptions($postString = '')
133
    {
134
        // Union custom Curl options and default.
135
        $options = array_replace(
136
            $this->options,
137
            $this->getDefaultHttpOptions()
138
        );
139
140
        if (!empty($postString)) {
141
            $options[CURLOPT_POST] = true;
142
            $options[CURLOPT_POSTFIELDS] = $postString;
143
        }
144
145
        return $options;
146
    }
147
148
    /**
149
     * Set custom Curl options to override default
150
     *
151
     * @param array $options
152
     * @return CurlHttpClient
153
     */
154
    public function setOptions(array $options)
155
    {
156
        $this->options = $options;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Get a cookie value by name
163
     *
164
     * @param $name
165
     * @return mixed
166
     */
167
    public function cookie($name)
168
    {
169
        return $this->cookies->get($name);
170
    }
171
172
    /**
173
     * Get all cookies
174
     *
175
     * @return array
176
     */
177
    public function cookies()
178
    {
179
        return $this->cookies->all();
180
    }
181
182
    /**
183
     * Init cookie file for a specified username. If username is empty we use
184
     * common cookie file for all sessions. If file does not exist it will
185
     * be created in system temp directory.
186
     *
187
     * @param $username
188
     * @return $this
189
     */
190
    protected function initCookieJar($username = '')
191
    {
192
        $this->cookieJar = $this->initCookieFile($username);
193
194
        return $this;
195
    }
196
197
    /**
198
     * Returns cookie file name by username. If username is empty we use a
199
     * random cookie name, to be sure we have different cookies
200
     * in parallel sessions.
201
     *
202
     * @param string $username
203
     * @return string
204
     */
205
    protected function initCookieFile($username)
206
    {
207
        if(empty($username)) {
208
            return tempnam(sys_get_temp_dir(), self::COOKIE_PREFIX);
209
        }
210
211
        $cookieName = self::COOKIE_PREFIX . $username;
212
        $cookieFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $cookieName;
213
214
        if (!file_exists($cookieFilePath)) {
215
            touch($cookieFilePath);
216
        }
217
218
        return $cookieFilePath;
219
    }
220
}