AbstractTarVersionProbe::getStatus()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 9
nop 0
dl 0
loc 36
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Compressy.
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 Gocobachi\Compressy\Adapter\VersionProbe;
14
15
use Gocobachi\Compressy\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
        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