ZipVersionProbe::getStatus()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 23
nc 7
nop 0
dl 0
loc 37
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
17
class ZipVersionProbe implements VersionProbeInterface
18
{
19
    private $isSupported;
20
    private $inflator;
21
    private $deflator;
22
23
    public function __construct(ProcessBuilderFactoryInterface $inflator, ProcessBuilderFactoryInterface $deflator)
24
    {
25
        $this->inflator = $inflator;
26
        $this->deflator = $deflator;
27
    }
28
29
    /**
30
     * Set the inflator to zip
31
     *
32
     * @param  ProcessBuilderFactoryInterface $inflator
33
     * @return ZipVersionProbe
34
     */
35
    public function setInflator(ProcessBuilderFactoryInterface $inflator)
36
    {
37
        $this->inflator = $inflator;
38
39
        return $this;
40
    }
41
42
    /**
43
     * Set the deflator to unzip
44
     *
45
     * @param  ProcessBuilderFactoryInterface $deflator
46
     * @return ZipVersionProbe
47
     */
48
    public function setDeflator(ProcessBuilderFactoryInterface $deflator)
49
    {
50
        $this->deflator = $deflator;
51
52
        return $this;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getStatus()
59
    {
60
        if (null !== $this->isSupported) {
61
            return $this->isSupported;
62
        }
63
64
        if (null === $this->inflator || null === $this->deflator) {
65
            return $this->isSupported = VersionProbeInterface::PROBE_NOTSUPPORTED;
66
        }
67
68
        $processDeflate = $this
69
            ->deflator
70
            ->create()
71
            ->add('-h')
72
            ->getProcess();
73
74
        $processDeflate->run();
75
76
        $processInflate = $this
77
            ->inflator
78
            ->create()
79
            ->add('-h')
80
            ->getProcess();
81
82
        $processInflate->run();
83
84
        if (false === $processDeflate->isSuccessful() || false === $processInflate->isSuccessful()) {
85
            return $this->isSupported = VersionProbeInterface::PROBE_NOTSUPPORTED;
86
        }
87
88
        $lines = explode("\n", $processInflate->getOutput(), 2);
89
        $inflatorOk = false !== stripos($lines[0], 'Info-ZIP');
90
91
        $lines = explode("\n", $processDeflate->getOutput(), 2);
92
        $deflatorOk = false !== stripos($lines[0], 'Info-ZIP');
93
94
        return $this->isSupported = ($inflatorOk && $deflatorOk) ? VersionProbeInterface::PROBE_OK : VersionProbeInterface::PROBE_NOTSUPPORTED;
95
    }
96
}
97