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

Downloader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 41.67%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 42
ccs 10
cts 24
cp 0.4167
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A sync() 0 18 4
A download() 0 13 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