ExtractArchiveCommand::execute()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 18
rs 9.8666
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 ExtractArchiveCommand extends BaseArchiveCommand
10
{
11
    protected static $defaultName = 'archive:extract';
12
13
    protected function configure()
14
    {
15
        parent::configure();
16
        $this
17
            ->setDescription('Extracts all archive contents and overwrites existing files')
18
            ->setHelp('Extracts all archive contents and overwrites existing files.')
19
            ->addArgument('outputDir', InputArgument::REQUIRED, 'Folder to extract contents')
20
        ;
21
    }
22
23
    public function execute(InputInterface $input, OutputInterface $output)
24
    {
25
        $output_dir = $input->getArgument('outputDir');
26
        if (!is_dir($output_dir)) {
27
            mkdir($output_dir, 0777, true);
28
        } else if (!is_writable($output_dir)) {
29
            chmod($output_dir, 0777);
30
        }
31
        $archive = $this->getArchive($input, $output);
32
33
        if (disk_free_space($output_dir) < $archive->getOriginalSize()) {
34
            throw new \LogicException('Archive size ' . implode($this->formatSize($archive->getOriginalSize())) . ' is more that available on disk '
35
                                      . implode($this->formatSize(disk_free_space($output_dir))));
36
        }
37
        $archive->extract($output_dir, $entry_selector, true);
38
        $output->writeln('<info>Extracted all archive contents (' . implode($this->formatSize($archive->getOriginalSize())) . ')</info>');
39
40
        return 0;
41
    }
42
}
43