ExtractFilesCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 15
rs 9.9332
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\Output\OutputInterface;
8
9
class ExtractFilesCommand extends BaseArchiveCommand
10
{
11
    protected static $defaultName = 'files:extract';
12
13
    protected function configure()
14
    {
15
        parent::configure();
16
        $this
17
            ->setDescription('Extracts archive contents by selector')
18
            ->setHelp('Extracts all archive contents by selector.')
19
            ->addArgument('outputDir', InputArgument::REQUIRED, 'Folder to extract contents')
20
            ->addArgument('entrySelector', InputArgument::OPTIONAL, 'Prefix of entry selector. Default is null, means root of archive')
21
        ;
22
    }
23
24
    public function execute(InputInterface $input, OutputInterface $output)
25
    {
26
        $output_dir = $input->getArgument('outputDir');
27
        if (!is_dir($output_dir)) {
28
            mkdir($output_dir, 0777, true);
29
        } else if (!is_writable($output_dir)) {
30
            chmod($output_dir, 0777);
31
        }
32
        $archive = $this->getArchive($input, $output);
33
        $entry_selector = $input->getArgument('entrySelector');
34
35
        $archive->extract($output_dir, $entry_selector, true);
36
        $output->writeln('<info>Extracted:</info> ' . implode(', ', $entry_selector) . ' (' . count($entry_selector) . ') file(s)');
37
38
        return 0;
39
    }
40
}
41