|
3 | 3 | namespace Camillebaronnet\ETL\Extractor; |
4 | 4 |
|
5 | 5 | use Camillebaronnet\ETL\Exception\BadInterface; |
| 6 | +use Camillebaronnet\ETL\Exception\DecoderNotFound; |
6 | 7 | use Camillebaronnet\ETL\Exception\MissingParameter; |
7 | 8 |
|
8 | 9 | class Http extends AbstractExtractor |
9 | 10 | { |
10 | | - /** |
11 | | - * Default context. |
12 | | - */ |
13 | | - public const DEFAULT_CONTEXT = [ |
14 | | - 'method' => 'GET', |
15 | | - 'data' => null, |
16 | | - 'headers' => [], |
17 | | - 'curl_opts' => [ |
18 | | - CURLOPT_RETURNTRANSFER => true, |
19 | | - CURLOPT_FOLLOWLOCATION => true, |
20 | | - CURLOPT_USERAGENT => '-', |
21 | | - CURLOPT_AUTOREFERER => true, |
22 | | - CURLOPT_SSL_VERIFYHOST => 2, |
23 | | - CURLOPT_SSL_VERIFYPEER => true, |
24 | | - ], |
| 11 | + public $method = 'GET'; |
| 12 | + public $url; |
| 13 | + public $data; |
| 14 | + public $curlOpts = [ |
| 15 | + CURLOPT_RETURNTRANSFER => true, |
| 16 | + CURLOPT_FOLLOWLOCATION => true, |
| 17 | + CURLOPT_USERAGENT => '-', |
| 18 | + CURLOPT_AUTOREFERER => true, |
| 19 | + CURLOPT_SSL_VERIFYHOST => 2, |
| 20 | + CURLOPT_SSL_VERIFYPEER => true, |
25 | 21 | ]; |
26 | 22 |
|
27 | 23 | /** |
28 | | - * @param array $context |
29 | | - * @return iterable |
30 | | - * @throws MissingParameter |
31 | 24 | * @throws BadInterface |
| 25 | + * @throws MissingParameter |
| 26 | + * @throws DecoderNotFound |
32 | 27 | */ |
33 | | - public function __invoke(array $context = []): iterable |
| 28 | + public function __invoke(): iterable |
34 | 29 | { |
35 | | - $this->requiredParameters(['url'], $context); |
| 30 | + $this->requiredParameters(['url']); |
36 | 31 |
|
37 | | - $context = array_merge(static::DEFAULT_CONTEXT, $context); |
38 | 32 | $curl_opts = [ |
39 | | - CURLOPT_URL => $context['url'], |
40 | | - CURLOPT_CUSTOMREQUEST => strtoupper($context['method']), |
| 33 | + CURLOPT_URL => $this->url, |
| 34 | + CURLOPT_CUSTOMREQUEST => strtoupper($this->method), |
41 | 35 | ]; |
42 | 36 |
|
43 | | - if (null !== $context['data']) { |
| 37 | + if (null !== $this->data) { |
44 | 38 | $curl_opts += [ |
45 | 39 | CURLOPT_POST => true, |
46 | | - CURLOPT_POSTFIELDS => $context['data'], |
| 40 | + CURLOPT_POSTFIELDS => $this->data, |
47 | 41 | ]; |
48 | 42 | } |
49 | 43 |
|
50 | | - if ('HEAD' === strtoupper($context['method'])) { |
| 44 | + if ('HEAD' === strtoupper($this->method)) { |
51 | 45 | $curl_opts += [CURLOPT_NOBODY => 1]; |
52 | 46 | } |
53 | 47 |
|
54 | 48 | $ch = curl_init(); |
55 | | - curl_setopt_array($ch, $curl_opts + $context['curl_opts']); |
| 49 | + curl_setopt_array($ch, $curl_opts + $this->curlOpts); |
56 | 50 | $content = curl_exec($ch); |
57 | 51 | $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); |
58 | 52 | curl_close($ch); |
59 | 53 |
|
60 | 54 | return $this->decode( |
61 | 55 | $content, |
62 | | - $contentType, |
63 | | - $context |
| 56 | + $contentType |
64 | 57 | ); |
65 | 58 | } |
66 | 59 | } |
0 commit comments