Completed
Push — master ( 2b75e8...156030 )
by Faiz
02:20
created

TranslationAddCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 2
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
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $translation = $input->getArgument('translation');
46
47
        if($translation === null){
48
            $this->translationTable($output, $this->allTranslations());
49
            $output->writeln("<info>Please specify a </info><comment>translation ID</comment><info>. You can refer to the table above.</info>\n");
50
        } else {
51
            $this->config->customTranslations($translation);
52
            // dump($this->config->get('translations'));
53
            $dw = new Downloader($this->config);
54
            $dw->sync();
55
            $output->writeln("<info>$translation</info> has been added successfully.\n");
56
        }
57
    }
58
59
    private function allTranslations()
60
    {
61
        $tanzil = new TanzilTranslations;
62
        return $tanzil->get();
63
    }
64
65
    private function translationTable($output, $translations)
66
    {
67
        $table = new Table($output);
68
        $table
69
            ->setHeaders(['ID', 'Language', 'Name', 'Translator'])
70
            ->setRows($translations)
71
        ;
72
        $table->render();
73
    }
74
}
75