Completed
Push — master ( ff973b...8fbee4 )
by C
05:50
created

UnzipCommand::is7zAvailable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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