Passed
Push — 1.1.x ( bc52f7...e8e75c )
by f
12:04
created

FormatsCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
c 1
b 0
f 0
dl 0
loc 81
rs 10
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
C execute() 0 67 13
1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Commands;
4
5
use Symfony\Component\Console\Helper\Table;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
10
use wapmorgan\UnifiedArchive\Formats;
11
12
class FormatsCommand extends BaseCommand
13
{
14
    protected static $defaultName = 'system:formats';
15
16
    protected function configure()
17
    {
18
        $this
19
            ->setDescription('Lists supported archive formats with current system configuration')
20
            ->setHelp('Lists supported archive formats with current system configuration. You can check supported formats and supported actions with them.')
21
            ->addArgument('driver', InputArgument::OPTIONAL, 'Filter formats support by specific driver')
22
            ->addUsage('\'wapmorgan\UnifiedArchive\Drivers\SevenZip\'')
23
        ;
24
    }
25
26
    public function execute(InputInterface $input, OutputInterface $output)
27
    {
28
        $table = new Table($output);
29
30
        /** @var string|BasicDriver $driver */
31
        $driver = $input->getArgument('driver');
32
33
        if ($driver !== null) {
0 ignored issues
show
introduced by
The condition $driver !== null is always true.
Loading history...
34
            $driver = $this->resolveDriverName($driver);
35
            if (!class_exists($driver) || !is_a($driver, BasicDriver::class, true)) {
36
                throw new \InvalidArgumentException('Class "' . $driver . '" not found or not in BasicDriver children');
37
            }
38
            $output->writeln('Supported formats by <info>' . $driver . '</info>');
39
40
            $table->setHeaders(['format', ...array_keys(self::$abilitiesLabels)]);
41
            foreach ($driver::getSupportedFormats() as $i => $format) {
42
                $abilities = $driver::checkFormatSupport($format);
43
                $row = [$format];
44
45
                foreach (self::$abilitiesLabels as $possibleAbility) {
46
                    $row[] = in_array($possibleAbility, $abilities, true) ? '+' : '';
47
                }
48
49
                $table->setRow($i, $row);
50
            }
51
            $table->render();
52
            return 0;
53
        }
54
55
        $formats = Formats::getSupportedDriverFormats();
56
        $headers = array_keys($formats);
57
        array_unshift($headers, 'driver type');
58
        array_unshift($headers, 'driver / format');
59
        $table->setHeaders($headers);
60
        $rows = [];
61
62
        /** @var BasicDriver $driverClass */
63
        foreach (Formats::$drivers as $driverClass) {
64
            $row = [
65
                substr($driverClass, strrpos($driverClass, '\\') + 1),
66
                BasicDriver::$typeLabels[$driverClass::TYPE],
67
            ];
68
            foreach ($formats as $format => $formatSupportStatus) {
69
                if (isset($formatSupportStatus[$driverClass])) {
70
                    $shortcuts = null;
71
                    foreach (self::$abilitiesShortCuts as $ability => $abilitiesShortCut) {
72
                        if (in_array($ability, $formatSupportStatus[$driverClass], true)) {
73
                            $shortcuts .= $abilitiesShortCut;
74
                        }
75
                    }
76
                    $row[] = $shortcuts;
77
                } else {
78
                    $row[] = '';
79
                }
80
            }
81
            $rows[] = $row;
82
        }
83
84
        $table->setRows($rows);
85
        //$table->setRow($i++, $row);
86
        $table->render();
87
88
        foreach (array_combine(array_values(self::$abilitiesShortCuts), array_keys(self::$abilitiesLabels)) as $shortCut => $label) {
89
            $output->writeln('<info>' . $shortCut . '</info> - ' . $label);
90
        }
91
92
        return 0;
93
    }
94
}
95