Completed
Push — master ( 998e37...ce7c33 )
by Faiz
01:51
created

XMLRepository::initialize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
crap 3
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 42
            $xmlFile = $this->firstMatchAvailableTranslation($translation);
36
37 42
        if ($xmlFile === false)
38 42
            throw new TranslationNotExists("Translation " . $translation . " didn't exists. Please check your config.");
39
40 39
        $xml = new XML($xmlFile);
41 39
        return $xml->find($surah, $ayah);
42
    }
43
44 57
    public function initialize()
45
    {
46
        // If original quran not exist in storage_path, copy one
47 57
        $source = realpath(__DIR__ . '/../../../data/ar.quran.xml');
48 57
        $dest = $this->config->get('storage_path') . '/ar.quran.xml';
49
50
        // If file not exist in `storage_path`, we copy one.
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
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
    /**
66
     * Get the first match xml from translation.
67
     *
68
     * @param string $translation Translation prefix or short form
69
     *
70
     * @return string|false String of path to the translation if exists, or false otherwise
71
     */
72 39
    private function firstMatchAvailableTranslation($translation)
73
    {
74 39
        $dir = new \DirectoryIterator($this->config->get('storage_path'));
75
76 39
        foreach ($dir as $fileinfo) {
77 39
            if (!$fileinfo->isDot()) {
78 39
                $filename = $fileinfo->getFilename();
79
80
                // If match the first file with translation prefix, return it
81 39
                $yes = preg_match('/^' . $translation . '/', $filename);
82
83 39
                if ($yes === 1)
84 39
                    return $this->config->get('storage_path') . '/' . $filename;
85 24
            }
86 39
        }
87
88 3
        return false;
89
    }
90
91
}