Completed
Push — master ( 998e37...26b154 )
by Faiz
01:50
created

XMLRepository::initialize()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

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