Completed
Push — master ( e8ecc6...4bc2d8 )
by Faiz
02:14
created

Downloader::sync()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 14
ccs 8
cts 10
cp 0.8
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3.072
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
                $url = 'http://tanzil.net/trans/?transID='.$tr.'&type='.$type;
26 26
                $this->download($url, $file);
27
            }
28 52
        }
29 78
    }
30
31
    public function download($url, $destination)
32
    {
33
        $client = new Client();
34
        $res = $client->get($url);
35
        file_put_contents($destination, $res->getBody());
36
    }
37
}
38