Completed
Pull Request — master (#132)
by
unknown
06:43 queued 04:56
created

ZipVersionProbe::setDeflator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
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 View Code Duplication
        if (null === $this->inflator || null === $this->deflator) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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