Passed
Push — master ( f73d2a...b6f14a )
by f
14:52
created

FormatCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 37 10
A configure() 0 6 1
1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Commands;
4
5
use Symfony\Component\Console\Helper\QuestionHelper;
6
use Symfony\Component\Console\Helper\Table;
7
use Symfony\Component\Console\Helper\TableCell;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\ChoiceQuestion;
12
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
13
use wapmorgan\UnifiedArchive\Formats;
14
15
class FormatCommand extends BaseCommand
16
{
17
    protected static $defaultName = 'system:format';
18
19
    protected function configure()
20
    {
21
        $this
22
            ->setDescription('Describes possible and actual format support')
23
            ->setHelp('Describes possible and actual format support.')
24
            ->addArgument('format', InputArgument::OPTIONAL, 'format')
25
        ;
26
    }
27
28
    /**
29
     * @throws \Exception
30
     */
31
    public function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        $formats = Formats::getAllPossibleSupportedFormats();
34
        $format = $input->getArgument('format');
35
36
        if (empty($format) || !isset($formats[$format])) {
37
            /** @var QuestionHelper $helper */
38
            $helper = $this->getHelper('question');
39
40
41
            $question = new ChoiceQuestion('Which format', array_keys($formats));
42
            $format = $helper->ask($input, $output, $question);
43
        }
44
        $output->writeln('Format <info>' . $format . '</info> drivers support');
45
46
        $table = new Table($output);
47
        $table->setHeaders(['driver', 'open', 'stream', 'create', 'append', 'update', 'encrypt']);
48
        /**
49
         * @var int $i
50
         * @var BasicDriver $driver
51
         */
52
        foreach ($formats[$format] as $i => $driver) {
53
            if ($driver::getInstallationInstruction() === null) {
54
                $table->setRow($i, [
55
                    $driver,
56
                    '+',
57
                    $driver::canStream($format) ? '+' : '',
58
                    $driver::canCreateArchive($format) ? '+' : '',
59
                    $driver::canAddFiles($format) ? '+' : '',
60
                    $driver::canDeleteFiles($format) ? '+' : '',
61
                    $driver::canEncrypt($format) ? '+' : '',
62
                ]);
63
            } else {
64
                $table->setRow($i, [$driver, new TableCell('<error>not installed</error>', ['colspan' => 6])]);
65
            }
66
        }
67
        $table->render();
68
    }
69
}
70