Issues (11)

src/Supports/Downloader.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace FaizShukri\Quran\Supports;
4
5
class Downloader
6
{
7
    private $config;
8
9 62
    public function __construct(Config $config)
10
    {
11 62
        $this->config = $config;
12 31
    }
13
14 60
    public function sync($type = 'xml')
15
    {
16 60
        $translations = (array) $this->config->get('translations');
17
18
        // Download quran data
19 60
        foreach ($translations as $tr) {
20 60
            $file = $this->config->get('storage_path') . '/' . $tr . '.' . $type;
0 ignored issues
show
Are you sure $this->config->get('storage_path') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
            $file = /** @scrutinizer ignore-type */ $this->config->get('storage_path') . '/' . $tr . '.' . $type;
Loading history...
21
22 60
            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
                $this->download($url, $file);
29
30
                if ($type == 'xml') {
31 30
                    $this->cleanXML($file);
32
                }
33
            }
34
        }
35 30
    }
36
37
    public function download($url, $destination)
38
    {
39
        $fp = fopen($destination, 'w+');
40
        $ch = curl_init();
41
        curl_setopt_array($ch, [
42
            CURLOPT_URL => $url,
43
            CURLOPT_FILE => $fp,
44
            CURLOPT_FOLLOWLOCATION => true
45
        ]);
46
        curl_exec($ch);
47
        curl_close($ch);
48
        fclose($fp);
49
    }
50
51 2
    public function cleanXML($file)
52
    {
53 2
        $str = file_get_contents($file);
54 2
        $re = '/(^\s*\#.*?(?=-))(-{2,})/m';
55 2
        preg_match($re, $str, $matches);
56
57 2
        $newStr = str_replace('-', '=', $matches[2]);
58 2
        $str = preg_replace($re, '$1' . $newStr, $str);
59
60 2
        file_put_contents($file, $str);
61 1
    }
62
}
63