TranslationAddCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace FaizShukri\Quran\Commands;
4
5
use FaizShukri\Quran\Supports\Config;
6
use FaizShukri\Quran\Supports\Downloader;
7
use FaizShukri\Quran\TanzilTranslations;
8
use FaizShukri\Quran\Repositories\Source\XMLRepository;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Helper\Table;
14
15
class TranslationAddCommand extends Command
16
{
17
    private $source;
18
19
    private $config;
20
21
    public function __construct()
22
    {
23
        parent::__construct();
24
        $this->config = new Config;
25
        $this->source = new XMLRepository;
26
        $this->source->setConfig($this->config);
27
    }
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('translation:add')
33
            ->setDescription('Add new translation')
34
            ->addArgument(
35
                'translation',
36
                InputArgument::OPTIONAL,
37
                'Translation to add'
38
            )
39
            ->addUsage('ms.basmeih');
40
    }
41
42
    protected function execute(InputInterface $input, OutputInterface $output): int
43
    {
44
        $translation = $input->getArgument('translation');
45
46
        if ($translation === null) {
47
            $this->translationTable($output, $this->allTranslations());
48
            $output->writeln("<info>Please specify a </info><comment>translation ID</comment><info>. You can refer to the table above.</info>\n");
49
        } else {
50
            $found = false;
51
            $translations = $this->allTranslations();
52
            foreach ($translations as $tr) {
53
                if ($translation == $tr['id']) {
54
                    $found = true;
55
                    break;
56
                }
57
            }
58
59
            if (!$found) {
60
                $this->translationTable($output, $translations);
61
                $output->writeln("<info>Invalid </info><comment>translation ID</comment><info>. Please refer to the available translation above.</info>\n");
62
                return 0;
63
            }
64
65
            $this->config->addTranslation($translation);
66
            $dw = new Downloader($this->config);
67
            $dw->sync();
68
            $output->writeln("<info>$translation</info> has been added successfully.\n");
69
        }
70
71
        return 0;
72
    }
73
74
    private function allTranslations()
75
    {
76
        $tanzil = new TanzilTranslations;
77
        return $tanzil->get();
78
    }
79
80
    private function translationTable($output, $translations)
81
    {
82
        $table = new Table($output);
83
        $table
84
            ->setHeaders(['ID', 'Language', 'Name', 'Translator'])
85
            ->setRows($translations);
86
        $table->render();
87
    }
88
}
89