Passed
Push — master ( 11d8e1...71f411 )
by f
13:53
created

FormatsCommand::execute()   D

Complexity

Conditions 18
Paths 8

Size

Total Lines 46
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 18
eloc 32
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 46
rs 4.8666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Commands;
4
5
use Symfony\Component\Console\Helper\Table;
6
use Symfony\Component\Console\Helper\TableCell;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
11
use wapmorgan\UnifiedArchive\Formats;
12
13
class FormatsCommand extends BaseCommand
14
{
15
    protected static $defaultName = 'system:formats';
16
17
    protected function configure()
18
    {
19
        $this
20
            ->setDescription('Lists supported archive formats with current system configuration')
21
            ->setHelp('Lists supported archive formats with current system configuration. You can check supported formats and supported actions with them.')
22
            ->addArgument('driver', InputArgument::OPTIONAL, 'Filter formats support by specific driver')
23
            ->addUsage('\'wapmorgan\UnifiedArchive\Drivers\SevenZip\'')
24
        ;
25
    }
26
27
    public function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        $table = new Table($output);
30
31
        /** @var string|BasicDriver $driver */
32
        $driver = $input->getArgument('driver');
33
34
        if ($driver !== null) {
0 ignored issues
show
introduced by
The condition $driver !== null is always true.
Loading history...
35
            if ($driver[0] !== '\\') {
36
                $driver = '\\'.$driver;
37
            }
38
            if (!class_exists($driver) || !is_a($driver, BasicDriver::class, true)) {
39
                throw new \InvalidArgumentException('Class "' . $driver . '" not found or not in BasicDriver children');
40
            }
41
            $output->writeln('Supported format by <info>' . $driver . '</info>');
42
43
            $table->setHeaders(['format', 'stream', 'create', 'append', 'update', 'encrypt']);
44
            foreach ($driver::getSupportedFormats() as $i => $format) {
45
                $table->setRow($i, [
46
                    $format,
47
                    $driver::canStream($format) ? '+' : '',
48
                    $driver::canCreateArchive($format) ? '+' : '',
49
                    $driver::canAddFiles($format) ? '+' : '',
50
                    $driver::canDeleteFiles($format) ? '+' : '',
51
                    $driver::canEncrypt($format) ? '+' : '',
52
                ]);
53
            }
54
            $table->render();
55
            return 0;
56
        }
57
58
        $table->setHeaders(['format', 'open', 'stream', 'create', 'append', 'update', 'encrypt', 'drivers']);
59
        $i = 0;
60
        foreach (Formats::getFormatsReport() as $format => $config) {
61
            $table->setRow($i++, [
62
                $format,
63
                $config['open'] ? '+' : '',
64
                $config['stream'] ? '+' : '',
65
                $config['create'] ? '+' : '',
66
                $config['append'] ? '+' : '',
67
                $config['update'] ? '+' : '',
68
                $config['encrypt'] ? '+' : '',
69
                new TableCell(implode("\n", $config['drivers']), ['rowspan' => count($config['drivers'])])
70
            ]);
71
        }
72
        $table->render();
73
    }
74
}
75