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

BaseArchiveCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A getArchive() 0 18 3
A getDriverFormatAbilities() 0 4 1
1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Commands;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
10
use wapmorgan\UnifiedArchive\Formats;
11
12
class BaseArchiveCommand extends BaseCommand
13
{
14
    protected function configure()
15
    {
16
        parent::configure();
17
        $this
18
            ->addArgument('archive', InputArgument::REQUIRED, 'Archive file')
19
            ->addOption('password', null, InputOption::VALUE_REQUIRED, 'Password for archive')
20
        ;
21
    }
22
23
    /**
24
     * @param InputInterface $input
25
     * @param OutputInterface $output
26
     * @return \wapmorgan\UnifiedArchive\UnifiedArchive
27
     * @throws \Exception
28
     */
29
    protected function getArchive(InputInterface $input, OutputInterface $output)
30
    {
31
        $file = realpath($input->getArgument('archive'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('archive') can also be of type null and string[]; however, parameter $path of realpath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        $file = realpath(/** @scrutinizer ignore-type */ $input->getArgument('archive'));
Loading history...
32
        $output->writeln('<comment>Opening ' . $file . '</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE);
33
        if (!is_file($file)) {
34
            throw new \InvalidArgumentException('File ' . $input->getArgument('archive') . ' is not accessible');
0 ignored issues
show
Bug introduced by
Are you sure $input->getArgument('archive') of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
            throw new \InvalidArgumentException('File ' . /** @scrutinizer ignore-type */ $input->getArgument('archive') . ' is not accessible');
Loading history...
35
        }
36
        $output->writeln('<comment>Format ' . Formats::detectArchiveFormat($file).'</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE);
37
        $password = $input->getOption('password');
38
        if (empty($password)) {
39
            $password = null;
40
        } else {
41
            $output->writeln('<comment>Passing password: ' . strlen($password).'</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE);
42
        }
43
        $archive = $this->open($file, $password);
44
        $output->writeln('<comment>Driver ' . $archive->getDriverType() . '</comment>', OutputInterface::VERBOSITY_VERBOSE);
45
        $output->writeln('<comment>Driver abilities: ' . implode(', ', $this->getDriverFormatAbilities($archive->getDriverType(), $archive->getFormat())) . '</comment>', OutputInterface::VERBOSITY_VERBOSE);
0 ignored issues
show
Bug introduced by
$archive->getDriverType() of type string is incompatible with the type wapmorgan\UnifiedArchive\Drivers\BasicDriver expected by parameter $driver of wapmorgan\UnifiedArchive...DriverFormatAbilities(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $output->writeln('<comment>Driver abilities: ' . implode(', ', $this->getDriverFormatAbilities(/** @scrutinizer ignore-type */ $archive->getDriverType(), $archive->getFormat())) . '</comment>', OutputInterface::VERBOSITY_VERBOSE);
Loading history...
46
        return $archive;
47
    }
48
49
    /**
50
     * @param BasicDriver $driver
51
     * @param $format
52
     * @return array
53
     */
54
    protected function getDriverFormatAbilities($driver, $format)
55
    {
56
        $abilities = $driver::checkFormatSupport($format);
57
        return array_keys(array_intersect(self::$abilitiesLabels, $abilities));
58
    }
59
}
60