Completed
Push — master ( a3f9bd...7a5180 )
by C
05:22
created

UnzipCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
namespace Tartana\Console\Command\Extract;
3
use Joomla\Registry\Registry;
4
use League\Flysystem\Adapter\AbstractAdapter;
5
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
6
use Tartana\Component\Command\Command;
7
use Tartana\Component\Command\Runner;
8
use Tartana\Util;
9
10
class UnzipCommand extends SevenzCommand
11
{
12
13
	private $executable = '';
14
15 11
	public function __construct (EventDispatcherInterface $dispatcher, Registry $configuration, Runner $runner)
16
	{
17
		// Setting the command name based on the class
18 11
		parent::__construct($dispatcher, $configuration);
19
20
		// On the DSM 6 7z is available only
21 11
		$cmd = new Command('which');
22 11
		$cmd->addArgument('7z');
23 11
		$cmd->setAsync(false);
24 11
		if ($runner->execute($cmd))
25
		{
26 3
			$this->executable = '7z';
27
		}
28
		else
29
		{
30 8
			$this->executable = 'unzip';
31
		}
32 11
	}
33
34 11
	protected function configure ()
35
	{
36 11
		parent::configure();
37
38 11
		$this->setName('unzip');
39 11
	}
40
41 9
	protected function isSuccessfullFinished ($output)
42
	{
43 9
		if ($this->executable == '7z')
44
		{
45 1
			return parent::isSuccessfullFinished($output);
46
		}
47
		else
48
		{
49 8
			return strpos($output, 'cannot find zipfile directory') === false && strpos($output, 'archive had fatal errors') === false;
50
		}
51
	}
52
53 9
	protected function getExtractCommand ($password, AbstractAdapter $source, AbstractAdapter $destination)
54
	{
55 9
		if ($this->executable == '7z')
56
		{
57 1
			return parent::getExtractCommand($password, $source, $destination);
58
		}
59
		else
60
		{
61
			/*
62
			 * The unzip command:
63
			 * -o: Overwrite
64
			 * -p: The password
65
			 * -d: Destination to extract to
66
			 */
67 8
			return 'unzip -o -P ' . escapeshellarg($password) . ' ' . escapeshellarg($source->applyPathPrefix('*.zip')) . ' -d ' .
68 8
					 $destination->getPathPrefix() . ' 2>&1';
69
		}
70
	}
71
72 8
	protected function getFilesToDelete (AbstractAdapter $source)
73
	{
74 8
		$filesToDelete = [];
75 8
		foreach ($source->listContents() as $file)
76
		{
77 7
			if (! Util::endsWith($file['path'], '.zip'))
78
			{
79 3
				continue;
80
			}
81
82 7
			$filesToDelete[] = $file['path'];
83
		}
84 8
		return $filesToDelete;
85
	}
86
}
87