Completed
Push — master ( 6695d4...9bc3b7 )
by Harry
04:41
created

Gzip::gunzip()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 24
loc 24
ccs 13
cts 13
cp 1
rs 8.9713
cc 2
eloc 14
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Graze\DataFile\Modify\Compress;
4
5
use Graze\DataFile\Helper\GetOptionTrait;
6
use Graze\DataFile\Helper\OptionalLoggerTrait;
7
use Graze\DataFile\Helper\Process\ProcessFactoryAwareInterface;
8
use Graze\DataFile\Helper\Process\ProcessTrait;
9
use Graze\DataFile\Modify\FileProcessTrait;
10
use Graze\DataFile\Node\FileNodeInterface;
11
use Graze\DataFile\Node\LocalFile;
12
use InvalidArgumentException;
13
use Psr\Log\LoggerAwareInterface;
14
use Psr\Log\LogLevel;
15
use Symfony\Component\Process\Exception\ProcessFailedException;
16
17 View Code Duplication
class Gzip implements CompressorInterface, DeCompressorInterface, LoggerAwareInterface, ProcessFactoryAwareInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
18
{
19
    use OptionalLoggerTrait;
20
    use FileProcessTrait;
21
    use GetOptionTrait;
22
23
    /**
24
     * Compress a file and return the new file
25
     *
26
     * @param FileNodeInterface $node
27
     * @param array             $options
28
     *
29
     * @return FileNodeInterface
30
     */
31 1
    public function compress(FileNodeInterface $node, array $options = [])
32
    {
33 1
        if (!($node instanceof LocalFile)) {
34 1
            throw new InvalidArgumentException("Node: $node should be a LocalFile");
35
        }
36
        return $this->gzip($node, $options);
37
    }
38
39
    /**
40
     * @param LocalFile $file
41
     * @param array     $options -keepOldFile <bool> (Default: true)
42
     *
43
     * @return LocalFile
44
     * @throws ProcessFailedException
45
     */
46 9
    public function gzip(LocalFile $file, array $options = [])
47
    {
48 9
        $this->options = $options;
49 9
        $pathInfo = pathinfo($file->getPath());
50
51 9
        if (!$file->exists()) {
52 1
            throw new InvalidArgumentException("The file: $file does not exist");
53
        }
54
55 8
        $outputFile = $file->getClone()
56 8
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.gz')
57 8
                           ->setCompression(CompressionType::GZIP);
58
59 8
        $this->log(LogLevel::INFO, "Compressing file: {file} into {target} using {compression}", [
60 8
            'file'        => $file,
61 8
            'target'      => $outputFile,
62
            'compression' => CompressionType::GZIP,
63
        ]);
64
65 8
        $cmd = "gzip -c {$file->getPath()} > {$outputFile->getPath()}";
66
67 8
        return $this->processFile($file, $outputFile, $cmd, $this->getOption('keepOldFile', true));
68
    }
69
70
    /**
71
     * Decompress a file and return the decompressed file
72
     *
73
     * @param FileNodeInterface $node
74
     * @param array             $options
75
     *
76
     * @return FileNodeInterface
77
     */
78 1
    public function decompress(FileNodeInterface $node, array $options = [])
79
    {
80 1
        if (!($node instanceof LocalFile)) {
81 1
            throw new InvalidArgumentException("Node: $node should be a LocalFile");
82
        }
83
        return $this->gunzip($node, $options);
84
    }
85
86
    /**
87
     * @extend Graze\DataFile\Node\File\LocalFile
88
     *
89
     * @param LocalFile $file
90
     * @param array     $options
91
     *
92
     * @return LocalFile
93
     */
94 5
    public function gunzip(LocalFile $file, array $options = [])
95
    {
96 5
        $this->options = $options;
97 5
        $pathInfo = pathinfo($file->getPath());
98
99 5
        if (!$file->exists()) {
100 1
            throw new InvalidArgumentException("The file: $file does not exist");
101
        }
102
103 4
        $outputFile = $file->getClone()
104 4
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'])
105 4
                           ->setCompression(CompressionType::NONE);
106
107 4
        $this->log(LogLevel::INFO, "DeCompressing file: {file} into {target} using {compression}", [
108 4
            'file'        => $file,
109 4
            'target'      => $outputFile,
110
            'compression' => CompressionType::GZIP,
111
        ]);
112
113 4
        $cmd = "gunzip -c {$file->getPath()} > {$outputFile->getPath()}";
114
115
116 4
        return $this->processFile($file, $outputFile, $cmd, $this->getOption('keepOldFile', true));
117
    }
118
}
119