Completed
Push — master ( 80ba95...38b123 )
by Faiz
03:17
created

Downloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace FaizShukri\Quran\Supports;
4
5
class Downloader
6
{
7
    private $config;
8
9 78
    public function __construct(Config $config)
10
    {
11 78
        $this->config = $config;
12 78
    }
13
14 78
    public function sync($type = 'xml')
15
    {
16 78
        $translations = (array) $this->config->get('translations');
17
18
        // Download quran data
19 78
        foreach ($translations as $tr) {
20 78
            $file = $this->config->get('storage_path') . '/' . $tr . '.' . $type;
21
22 78
            if (!file_exists($file)) {
23
                if (php_sapi_name() === 'cli') {
24
                    echo "Downloading translation \e[32m$tr\e[0m ...\n";
25
                }
26
27
                $url = 'http://tanzil.net/trans/?transID=' . $tr . '&type=' . $type;
28 26
                $this->download($url, $file);
29
            }
30
        }
31 78
    }
32
33
    public function download($url, $destination)
34
    {
35
        $fp = fopen($destination, 'w+');
36
        $ch = curl_init();
37
        curl_setopt_array($ch, [
38
            CURLOPT_URL => $url,
39
            CURLOPT_FILE => $fp,
40
            CURLOPT_FOLLOWLOCATION => true
41
        ]);
42
        curl_exec($ch);
43
        curl_close($ch);
44
        fclose($fp);
45
    }
46
}
47