Completed
Push — master ( 244f91...2e9768 )
by Faiz
01:41
created

QuranCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 14 3
B parseResult() 0 24 5
A buildAyah() 0 8 2
1
#!/usr/bin/php
2
<?php
3
4
require_once(realpath('vendor/autoload.php'));
5
6
use FaizShukri\Quran\Quran;
7
8
class QuranCommand
9
{
10
    public function execute(array $args)
11
    {
12
        $quran = new Quran;
13
14
        try {
15
            if(isset($args[2])) $quran->translation($args[2]);
16
            $ayah = $quran->get($args[1]);
17
18
            return $this->parseResult($ayah) . "\n";
19
20
        } catch(Exception $e) {
21
            return "Error: " . $e->getMessage() . "\n";
22
        }
23
    }
24
25
    private function parseResult($args)
26
    {
27
        // Just a single ayah is return. No need to parse anything.
28
        if(is_string($args)) return $args . "\n";
29
30
        // Multiple ayah/one surah or multiple surah/one ayah. Not both.
31
        if(is_string(current($args))) return $this->buildAyah($args);
32
33
        // Both multiple ayah and multiple surah.
34
        $count = 0;
35
        $result = "\n";
36
37
        foreach($args as $translation => $aya) {
38
39
            $result .= strtoupper($translation) . "\n" . str_repeat('=', strlen($translation) + 2) . "\n\n";
40
            $result .= $this->buildAyah($aya);
41
42
            ++$count;
43
            if( $count < sizeof($args) ) $result .= "\n\n";
44
        }
45
46
        return $result;
47
48
    }
49
50
    private function buildAyah($ayah)
51
    {
52
        $result = "";
53
        foreach($ayah as $num => $aya) {
54
            $result .= "[ " . strtoupper($num) . " ]\t" . $aya . "\n";
55
        }
56
        return $result;
57
    }
58
59
}
60
61
$command = new QuranCommand;
62
echo $command->execute($argv);
63