Completed
Pull Request — master (#182)
by Sergey
05:28 queued 02:24
created

CurlHttpClient::getCookieFilePath()   A

Complexity

Conditions 3
Paths 3

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