Completed
Push — master ( 0392b2...900bb9 )
by Faiz
01:45
created

QuranCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

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

4 Methods

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

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
63
    }
64
65
}
66
67
$command = new QuranCommand;
68
echo $command->execute($argv);
69