Completed
Push — master ( df3dac...106a09 )
by Faiz
02:48
created

Downloader::sync()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

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