Completed
Push — master ( b59592...c78116 )
by Faiz
01:47
created

XMLRepository::getAllSurah()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace FaizShukri\Quran\Repositories\Source;
4
5
use FaizShukri\Quran\Exceptions\TranslationNotExists;
6
use FaizShukri\Quran\Supports\Config;
7
use FaizShukri\Quran\Supports\Downloader;
8
use FaizShukri\Quran\Supports\XML;
9
10
class XMLRepository implements SourceInterface
11
{
12
    private $config;
13
14 57
    public function setConfig(Config $config)
15
    {
16 57
        $this->config = $config;
17 57
    }
18
19
    public function getAllSurah()
20
    {
21
        // TODO: Implement getAllSurah() method.
22
    }
23
24
    public function getSurah($surah)
25
    {
26
        // TODO: Implement getSurah() method.
27
    }
28
29 42
    public function getAyah($surah, $ayah, $translation = 'ar')
30
    {
31 42
        $xmlFile = $this->config->get('storage_path') . '/' . $translation . '.xml';
32
33
        // If files not exist, get the first match
34 42
        if (!file_exists($xmlFile)) {
35 39
            $xmlFile = $this->firstMatchAvailableTranslation($translation);
36 39
        }
37
38 42
        if ($xmlFile === false) {
39 3
            throw new TranslationNotExists("Translation " . $translation . " didn't exists. Please check your config.");
40
        }
41
42 39
        $xml = new XML($xmlFile);
43 39
        return $xml->find($surah, $ayah);
44
    }
45
46 57
    public function initialize()
47
    {
48
        // If original quran not exist in storage_path, copy one
49 57
        $source = realpath(__DIR__ . '/../../../data/ar.quran.xml');
50 57
        $dest = $this->config->get('storage_path') . '/ar.quran.xml';
51 57
        if (!file_exists($dest)) {
52
53
            // If storage path didn't exist, create it
54 3
            if (!file_exists($this->config->get('storage_path'))) {
55 3
                mkdir($this->config->get('storage_path'));
56 3
            }
57 3
            copy($source, $dest);
58 3
        }
59
60
        // Sync translation files to storage path
61 57
        $dw = new Downloader($this->config);
62 57
        $dw->sync();
63 57
    }
64
65 39
    private function firstMatchAvailableTranslation($translation)
66
    {
67 39
        $dir = new \DirectoryIterator($this->config->get('storage_path'));
68
69 39
        foreach ($dir as $fileinfo) {
70 39
            if (!$fileinfo->isDot()) {
71 39
                $filename = $fileinfo->getFilename();
72
73
                // If match the first file with translation prefix, return it
74 39
                $yes = preg_match('/^' . $translation . '/', $filename);
75 39
                if ($yes === 1) {
76 36
                    return $this->config->get('storage_path') . '/' . $filename;
77
                }
78 24
            }
79 39
        }
80
81 3
        return false;
82
    }
83
84
}