Completed
Push — master ( 252970...4a8c07 )
by Faiz
02:03
created

Downloader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 58.81%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 5
c 2
b 1
f 1
lcom 1
cbo 3
dl 0
loc 29
ccs 10
cts 17
cp 0.5881
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A download() 0 6 1
A sync() 0 12 3
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
        // Download quran data
19 78
        foreach ($this->config->get('translations') as $tr) {
0 ignored issues
show
Bug introduced by
The expression $this->config->get('translations') of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
20 78
            $file = $this->config->get('storage_path').'/'.$tr.'.'.$type;
21
22 78
            if (!file_exists($file)) {
23
                $url = 'http://tanzil.net/trans/?transID='.$tr.'&type='.$type;
24 26
                $this->download($url, $file);
25
            }
26 52
        }
27 78
    }
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