Completed
Push — master ( 15cc07...1c3d15 )
by Faiz
04:44 queued 02:21
created

Downloader::sync()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.3332

Importance

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