1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Zippy. |
5
|
|
|
* |
6
|
|
|
* (c) Alchemy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Alchemy\Zippy\Adapter\VersionProbe; |
14
|
|
|
|
15
|
|
|
use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface; |
16
|
|
|
use Symfony\Component\Process\Process; |
17
|
|
|
|
18
|
|
|
abstract class AbstractTarVersionProbe implements VersionProbeInterface |
19
|
|
|
{ |
20
|
|
|
private $isSupported; |
21
|
|
|
private $inflator; |
22
|
|
|
private $deflator; |
23
|
|
|
|
24
|
|
|
public function __construct(ProcessBuilderFactoryInterface $inflator, ProcessBuilderFactoryInterface $deflator) |
25
|
|
|
{ |
26
|
|
|
$this->inflator = $inflator; |
27
|
|
|
$this->deflator = $deflator; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function getStatus() |
34
|
|
|
{ |
35
|
|
|
if (null !== $this->isSupported) { |
36
|
|
|
return $this->isSupported; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
if (null === $this->inflator || null === $this->deflator) { |
|
|
|
|
40
|
|
|
return $this->isSupported = VersionProbeInterface::PROBE_NOTSUPPORTED; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$good = true; |
44
|
|
|
|
45
|
|
|
foreach (array($this->inflator, $this->deflator) as $builder) { |
46
|
|
|
/** @var Process $process */ |
47
|
|
|
$process = $builder |
48
|
|
|
->create() |
49
|
|
|
->add('--version') |
50
|
|
|
->getProcess(); |
51
|
|
|
|
52
|
|
|
$process->run(); |
53
|
|
|
|
54
|
|
|
if (!$process->isSuccessful()) { |
55
|
|
|
return $this->isSupported = VersionProbeInterface::PROBE_NOTSUPPORTED; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$lines = explode("\n", $process->getOutput(), 2); |
59
|
|
|
$good = false !== stripos($lines[0], $this->getVersionSignature()); |
60
|
|
|
|
61
|
|
|
if (!$good) { |
62
|
|
|
break; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->isSupported = $good ? VersionProbeInterface::PROBE_OK : VersionProbeInterface::PROBE_NOTSUPPORTED; |
67
|
|
|
|
68
|
|
|
return $this->isSupported; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the signature of inflator/deflator |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
abstract protected function getVersionSignature(); |
77
|
|
|
} |
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.