Completed
Push — master ( 8112b0...bcd086 )
by Faiz
02:39
created

XMLRepository::initialize()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 28
rs 8.5806
ccs 0
cts 17
cp 0
cc 4
eloc 13
nc 8
nop 0
crap 20
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
    public function setConfig(Config $config)
15
    {
16
        $this->config = $config;
17
    }
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
    public function initialize()
81
    {
82
        // If original quran not exist in storage_path, copy one
83
        $quran_source = realpath(__DIR__ . '/../../../data/ar.quran.xml');
84
        $quran_dest = $this->config->get('storage_path') . '/ar.quran.xml';
85
86
        $meta_source = realpath(__DIR__ . '/../../../data/quran-data.xml');
87
        $meta_dest = $this->config->get('storage_path') . '/quran-data.xml';
88
89
        // If storage path didn't exist, create it
90
        if (!file_exists($this->config->get('storage_path'))) {
91
            mkdir($this->config->get('storage_path'));
92
        }
93
94
        // Copy quran
95
        if (!file_exists($quran_dest)) {
96
            copy($quran_source, $quran_dest);
97
        }
98
99
        // Copy metadata
100
        if (!file_exists($meta_dest)) {
101
            copy($meta_source, $meta_dest);
102
        }
103
104
        // Sync translation files to storage path
105
        $dw = new Downloader($this->config);
106
        $dw->sync();
107
    }
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
}