Completed
Push — master ( bcd086...46fbfd )
by Faiz
03:15
created

XMLRepository::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 66
    public function setConfig(Config $config)
15
    {
16 66
        $this->config = $config;
17 66
    }
18
19 3
    public function chapters()
20
    {
21 3
        $xmlFile = $this->config->get('storage_path') . '/quran-data.xml';
22 3
        $xml = new XML($xmlFile);
23 3
        $result = [];
24
25 3
        $xpath = '//suras/sura';
26 3
        $xpathResult = $xml->find($xpath);
27
28 3
        while (list(, $sura) = each($xpathResult)) {
29 3
            $sura = (array)$sura;
30 3
            $sura = current($sura);
31 3
            $result[$sura['index']] = (object) $sura;
32 3
        }
33
34 3
        return $result;
35
    }
36
37 3
    public function chapter($chapter)
38
    {
39 3
        $xmlFile = $this->config->get('storage_path') . '/quran-data.xml';
40 3
        $xml = new XML($xmlFile);
41
42 3
        $xpath = "//suras/sura[@index=$chapter]";
43 3
        $xpathResult = $xml->find($xpath);
44
45 3
        $sura = (array) current($xpathResult);
46 3
        return (object) $sura['@attributes'];
47
    }
48
49 42
    public function ayah($surah, $ayah, $translation = 'ar')
50
    {
51 42
        $xmlFile = $this->config->get('storage_path') . '/' . $translation . '.xml';
52
53
        // If files not exist, get the first match
54 42
        if (!file_exists($xmlFile)) {
55 39
            $xmlFile = $this->firstMatchAvailableTranslation($translation);
56 39
        }
57
58 42
        if ($xmlFile === false) {
59 3
            throw new TranslationNotExists("Translation " . $translation . " didn't exists. Please check your config.");
60
        }
61
62 39
        $xml = new XML($xmlFile);
63 39
        $result = [];
64
65 39
        $xpath = '//sura[@index=' . $surah . ']/aya[' . implode(' or ', array_map(function ($a) {
66 39
                return '@index=' . $a;
67 39
            }, $ayah)) . ']';
68
69 39
        $xpathResult = $xml->find($xpath);
70
71 39
        while (list(, $node) = each($xpathResult)) {
72 39
            $node = (array)$node;
73 39
            $verse = current($node);
74 39
            $result[$verse['index']] = $verse['text'];
75 39
        }
76
77 39
        return $result;
78
    }
79
80 66
    public function initialize()
81
    {
82
        // If original quran not exist in storage_path, copy one
83 66
        $quran_source = realpath(__DIR__ . '/../../../data/ar.quran.xml');
84 66
        $quran_dest = $this->config->get('storage_path') . '/ar.quran.xml';
85
86 66
        $meta_source = realpath(__DIR__ . '/../../../data/quran-data.xml');
87 66
        $meta_dest = $this->config->get('storage_path') . '/quran-data.xml';
88
89
        // If storage path didn't exist, create it
90 66
        if (!file_exists($this->config->get('storage_path'))) {
91
            mkdir($this->config->get('storage_path'));
92
        }
93
94
        // Copy quran
95 66
        if (!file_exists($quran_dest)) {
96
            copy($quran_source, $quran_dest);
97
        }
98
99
        // Copy metadata
100 66
        if (!file_exists($meta_dest)) {
101
            copy($meta_source, $meta_dest);
102
        }
103
104
        // Sync translation files to storage path
105 66
        $dw = new Downloader($this->config);
106 66
        $dw->sync();
107 66
    }
108
109
    /**
110
     * Get the first match xml from translation.
111
     *
112
     * @param string $translation Translation prefix or short form
113
     *
114
     * @return string|false String of path to the translation if exists, or false otherwise
115
     */
116 39
    private function firstMatchAvailableTranslation($translation)
117
    {
118 39
        $dir = new \DirectoryIterator($this->config->get('storage_path'));
119
120 39
        foreach ($dir as $fileinfo) {
121 39
            if (!$fileinfo->isDot()) {
122 39
                $filename = $fileinfo->getFilename();
123
124
                // If match the first file with translation prefix, return it
125 39
                $yes = preg_match('/^' . $translation . '/', $filename);
126 39
                if ($yes === 1) {
127 36
                    return $this->config->get('storage_path') . '/' . $filename;
128
                }
129 24
            }
130 39
        }
131
132 3
        return false;
133
    }
134
135
}