Completed
Pull Request — master (#150)
by Sergey
03:09
created

CurlHttpClient   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 175
wmc 14
lcom 1
cbo 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 11 1
A getErrors() 0 4 1
A getToken() 0 4 1
A setUserAgent() 0 8 2
A close() 0 4 1
A init() 0 11 1
A getDefaultHttpOptions() 0 14 1
A makeHttpOptions() 0 18 4
A setOptions() 0 6 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api;
4
5
use seregazhuk\PinterestBot\Api\Contracts\HttpClient;
6
use seregazhuk\PinterestBot\Helpers\CsrfHelper;
7
use seregazhuk\PinterestBot\Helpers\UrlHelper;
8
9
/**
10
 * Class CurlAdapter.
11
 */
12
class CurlHttpClient implements HttpClient
13
{
14
    public $cookieName = 'pinterest_cookie';
15
16
    /**
17
     * Custom CURL options for requests.
18
     *
19
     * @var array
20
     */
21
    protected $options = [];
22
23
    /**
24
     * @var string
25
     */
26
    protected $userAgent = 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0';
27
28
    /**
29
     * @var array
30
     */
31
    protected $headers = [];
32
33
    /**
34
     * Contains the curl instance.
35
     *
36
     * @var resource
37
     */
38
    protected $curl;
39
40
    /**
41
     * @var string
42
     */
43
    protected $cookieJar;
44
45
    public function __construct()
46
    {
47
        $this->cookieJar = tempnam(sys_get_temp_dir(), $this->cookieName);
48
    }
49
50
    /**
51
     * Executes curl request.
52
     *
53
     * @param string $url
54
     * @param string $postString
55
     * @param array $headers
56
     * @return string
57
     */
58
    public function execute($url, $postString = '', array $headers = [])
59
    {
60
        $this->headers = $headers;
61
62
        $this->init($url, $postString);
63
64
        $res = curl_exec($this->curl);
65
        $this->close();
66
67
        return $res;
68
    }
69
70
    /**
71
     * Get curl errors.
72
     *
73
     * @return string
74
     */
75
    public function getErrors()
76
    {
77
        return curl_error($this->curl);
78
    }
79
80
    /**
81
     * @return null|string
82
     */
83
    public function getToken()
84
    {
85
        return CsrfHelper::getTokenFromFile($this->cookieJar);
86
    }
87
88
    /**
89
     * @param string $userAgent
90
     * @return $this
91
     */
92
    public function setUserAgent($userAgent)
93
    {
94
        if ($userAgent !== null) {
95
            $this->userAgent = $userAgent;
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * Close the curl resource.
103
     *
104
     * @return void
105
     */
106
    protected function close()
107
    {
108
        curl_close($this->curl);
109
    }
110
111
    /**
112
     * Initializes curl resource with options.
113
     *
114
     * @param string $url
115
     * @param $postString
116
     * @return $this
117
     */
118
    protected function init($url, $postString)
119
    {
120
        $this->curl = curl_init($url);
121
122
        curl_setopt_array(
123
            $this->curl,
124
            $this->makeHttpOptions($postString)
125
        );
126
127
        return $this;
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    protected function getDefaultHttpOptions()
134
    {
135
        return [
136
            CURLOPT_USERAGENT      => $this->userAgent,
137
            CURLOPT_RETURNTRANSFER => true,
138
            CURLOPT_SSL_VERIFYPEER => false,
139
            CURLOPT_FOLLOWLOCATION => true,
140
            CURLOPT_ENCODING       => 'gzip,deflate',
141
            CURLOPT_HTTPHEADER     => $this->headers,
142
            CURLOPT_REFERER        => UrlHelper::URL_BASE,
143
            CURLOPT_COOKIEFILE     => $this->cookieJar,
144
            CURLOPT_COOKIEJAR      => $this->cookieJar,
145
        ];
146
    }
147
148
    /**
149
     * Adds necessary curl options for query.
150
     *
151
     * @param string $postString POST query string
152
     *
153
     * @return array
154
     */
155
    protected function makeHttpOptions($postString = '')
156
    {
157
        $options = $this->getDefaultHttpOptions();
158
159
        if (!empty($postString)) {
160
            $options[CURLOPT_POST] = true;
161
            $options[CURLOPT_POSTFIELDS] = $postString;
162
        }
163
164
        // Override default options with custom
165
        if (!empty($this->options)) {
166
            foreach ($this->options as $option => $value) {
167
                $options[$option] = $value;
168
            }
169
        }
170
171
        return $options;
172
    }
173
174
    /**
175
     * Set custom Curl options to override default
176
     *
177
     * @param array $options
178
     * @return CurlHttpClient
179
     */
180
    public function setOptions(array $options)
181
    {
182
        $this->options = $options;
183
184
        return $this;
185
    }
186
}
187