BaseFileCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getArchiveAndFile() 0 18 3
A configure() 0 5 1
1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Commands;
4
5
use Symfony\Component\Console\Helper\QuestionHelper;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ChoiceQuestion;
10
11
class BaseFileCommand extends BaseArchiveCommand
12
{
13
    protected function configure()
14
    {
15
        parent::configure();
16
        $this
17
            ->addArgument('file', InputArgument::OPTIONAL, 'Archive entry')
18
        ;
19
    }
20
21
    /**
22
     * @param InputInterface $input
23
     * @param OutputInterface $output
24
     * @return array
25
     * @throws \Exception
26
     */
27
    protected function getArchiveAndFile(InputInterface $input, OutputInterface $output)
28
    {
29
        $archive = $this->getArchive($input, $output);
30
        $file = $input->getArgument('file');
31
        $files = $archive->getFiles();
32
33
        if (empty($file)) {
34
            /** @var QuestionHelper $helper */
35
            $helper = $this->getHelper('question');
36
37
            $question = new ChoiceQuestion('Which file', $files);
38
            $file = $helper->ask($input, $output, $question);
39
        } else if (!in_array($file, $files, true)) {
40
            throw new \InvalidArgumentException('File "' . $file . '" not found in archive');
41
        }
42
        $output->writeln('<comment>Selecting file ' . $file . '</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE);
43
44
        return [$archive, $file];
45
    }
46
}
47