Passed
Push — master ( e9611b...a3681d )
by f
13:07
created

DriversCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 55 7
A configure() 0 5 1
A formatInstallation() 0 7 1
1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Commands;
4
5
use Symfony\Component\Console\Helper\FormatterHelper;
6
use Symfony\Component\Console\Helper\HelperSet;
7
use Symfony\Component\Console\Helper\QuestionHelper;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
13
use wapmorgan\UnifiedArchive\Formats;
14
15
class DriversCommand extends BaseCommand
16
{
17
    protected static $defaultName = 'system:drivers';
18
19
    protected function configure()
20
    {
21
        $this
22
            ->setDescription('Lists supported drivers for different formats and their installation status in system')
23
            ->setHelp('Lists supported drivers for different formats and their installation status in system. You need to manually install any of them.')
24
        ;
25
    }
26
27
    public function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        $notInstalled = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $notInstalled is dead and can be removed.
Loading history...
30
        $drivers = [];
31
        /** @var FormatterHelper $formatter */
32
        $formatter = $this->getHelper('formatter');
0 ignored issues
show
Unused Code introduced by
The assignment to $formatter is dead and can be removed.
Loading history...
33
34
        /** @var BasicDriver $driverClass */
35
        foreach (Formats::$drivers as $driverClass) {
36
            $description = $driverClass::getDescription();
37
            if (!$driverClass::isInstalled()) {
38
                $drivers[$driverClass::TYPE][$driverClass] = [$description, false, $driverClass::getInstallationInstruction()];
39
//                $notInstalled[] = [$driverClass, $description, $install];
40
            } else {
41
                $drivers[$driverClass::TYPE][$driverClass] = [$description, true];
42
            }
43
44
//            if (!empty($install)) {
45
//            } else {
46
//                $output->writeln($formatter->formatSection($driverClass, $description) . ': ' . implode(', ', $driverClass::getSupportedFormats()));
47
//            }
48
        }
49
50
        /** @var FormatterHelper $formatter */
51
        $formatter = $this->getHelper('formatter');
52
        $type_match_rules = [
53
            BasicDriver::TYPE_EXTENSION => 'php extension',
54
            BasicDriver::TYPE_UTILITIES => 'utilities + php bridge',
55
            BasicDriver::TYPE_PURE_PHP => 'pure php',
56
        ];
57
58
        foreach ($drivers as $type => $typeDrivers) {
59
            foreach ($typeDrivers as $typeDriverClass => $typeDriverConfig) {
60
                $type_messages = [];
61
                if ($typeDriverConfig[1]) {
62
                    $type_messages[] = '<info>' . $typeDriverClass . '</info>: ' . $typeDriverConfig[0];
63
                } else {
64
                    $type_messages[] = '<error>' . $typeDriverClass . '</error>: ' . $typeDriverConfig[0];
65
                    $type_messages[] = $this->formatInstallation($typeDriverConfig[2], 4);
66
                }
67
                $output->writeln($formatter->formatSection(
68
                    $type_match_rules[$type],
69
                    implode("\n", $type_messages), $typeDriverConfig[1] ? 'info' : 'error'));
70
            }
71
        }
72
73
//        if (!empty($notInstalled)) {
74
//            foreach ($notInstalled as $data) {
75
//                $output->writeln($formatter->formatSection($data[0], $data[1] . ': ' . implode(', ', $data[0]::getSupportedFormats()), 'error'));
76
//                $data[2] = preg_replace('~`(.+?)`~', '<options=bold,underscore>$1</>', $data[2]);
77
//                $output->writeln($formatter->formatSection($data[0], $data[2], 'comment'));
78
//            }
79
//        }
80
81
        return 0;
82
    }
83
84
    protected function formatInstallation($doc, $leftPadding = 4)
85
    {
86
        return implode("\n", array_map(
87
            function($line) use ($leftPadding) { return str_repeat(' ', $leftPadding) . $line; },
88
            explode(
89
                "\n",
90
                preg_replace('~`(.+?)`~', '<options=bold,underscore>$1</>', $doc)
91
            )
92
        ));
93
    }
94
}
95