FileDownloader::download()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
rs 9.7
cc 3
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppUtils\FileHelper;
6
7
use AppUtils\FileHelper;
8
use AppUtils\FileHelper_Exception;
9
use AppUtils\RequestHelper;
10
11
class FileDownloader
12
{
13
    /**
14
     * @var string
15
     */
16
    private $url;
17
18
    /**
19
     * @var int
20
     */
21
    private $timeout = 14;
22
23
    /**
24
     * @var bool
25
     */
26
    private $SSLEnabled = true;
27
28
    private function __construct(string $url)
29
    {
30
        $this->url = $url;
31
    }
32
33
    public static function factory(string $url) : FileDownloader
34
    {
35
        return new FileDownloader($url);
36
    }
37
38
    /**
39
     * @param int $timeout
40
     * @return FileDownloader
41
     */
42
    public function setTimeout(int $timeout) : FileDownloader
43
    {
44
        if($timeout > 0)
45
        {
46
            $this->timeout = $timeout;
47
        }
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param bool $enabled
54
     * @return FileDownloader
55
     */
56
    public function setSSLEnabled(bool $enabled=true) : FileDownloader
57
    {
58
        $this->SSLEnabled = $enabled;
59
        return $this;
60
    }
61
62
    /**
63
     * Uses cURL to download the contents of the specified URL,
64
     * returns the content.
65
     *
66
     * @throws FileHelper_Exception
67
     * @return string
68
     *
69
     * @see FileHelper::ERROR_CANNOT_OPEN_URL
70
     */
71
    public function download() : string
72
    {
73
        $ch = $this->initCurl();
74
75
        $output = curl_exec($ch);
76
77
        if($output === false)
78
        {
79
            throw new FileHelper_Exception(
80
                'Unable to open URL',
81
                sprintf(
82
                    'Tried accessing URL "%1$s" using cURL, but the request failed. cURL error: %2$s',
83
                    $this->url,
84
                    curl_error($ch)
85
                ),
86
                FileHelper::ERROR_CANNOT_OPEN_URL
87
            );
88
        }
89
90
        curl_close($ch);
91
92
        if(is_string($output))
93
        {
94
            return $output;
95
        }
96
97
        throw new FileHelper_Exception(
98
            'Unexpected cURL output.',
99
            'The cURL output is not a string, although the CURLOPT_RETURNTRANSFER option is set.',
100
            FileHelper::ERROR_CURL_OUTPUT_NOT_STRING
101
        );
102
    }
103
104
    /**
105
     * @return resource
106
     * @throws FileHelper_Exception
107
     */
108
    private function initCurl()
109
    {
110
        $ch = RequestHelper::createCURL();
111
112
        curl_setopt($ch, CURLOPT_URL, $this->url);
113
        curl_setopt($ch, CURLOPT_REFERER, $this->url);
114
        curl_setopt($ch, CURLOPT_USERAGENT, "Google Chrome/1.0");
115
        curl_setopt($ch, CURLOPT_HEADER, 0);
116
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
117
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
118
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
119
120
        if(!$this->SSLEnabled)
121
        {
122
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
123
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
124
        }
125
126
        return $ch;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ch also could return the type CurlHandle which is incompatible with the documented return type resource.
Loading history...
127
    }
128
}
129