DownloaderUtil   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 14
c 2
b 0
f 1
dl 0
loc 30
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A downloadURL() 0 22 6
1
<?php
2
3
namespace Jackal\Downloader\Utils;
4
5
use Jackal\Downloader\Exception\DownloadException;
6
7
class DownloaderUtil
8
{
9
    /**
10
     * @param string $url
11
     * @param string $outfile
12
     * @param callable|null $callback
13
     * @throws DownloadException
14
     */
15
    public static function downloadURL(string $url, string $outfile, ?callable $callback = null) : void
16
    {
17
        if (($rh = fopen($url, 'rb')) === false) {
18
            throw DownloadException::downloadError('Error opening file: ' . $url);
19
        }
20
21
        if(($wh = fopen($outfile, 'wb')) === false){
22
            throw DownloadException::downloadError('Error creating file: ' . $outfile);
23
        }
24
25
        $kbRead = 0;
26
        while (!feof($rh)) {
27
            $kbRead++;
28
            if (fwrite($wh, fread($rh, 1024)) === false) {
29
                throw DownloadException::downloadError('Cannot write to file: ' . $outfile);
30
            }
31
            if ($callback !== null) {
32
                $callback($kbRead);
33
            }
34
        }
35
        fclose($rh);
36
        fclose($wh);
37
    }
38
}
39