Completed
Push — master ( 9bc3b7...a15851 )
by Harry
02:46
created

Gzip::decompress()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 16

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 27
loc 27
ccs 15
cts 15
cp 1
rs 8.8571
cc 3
eloc 16
nc 3
nop 2
crap 3
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\Modify\FileProcessTrait;
9
use Graze\DataFile\Node\FileNodeInterface;
10
use Graze\DataFile\Node\LocalFile;
11
use InvalidArgumentException;
12
use Psr\Log\LoggerAwareInterface;
13
use Psr\Log\LogLevel;
14
15 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...
16
{
17
    use OptionalLoggerTrait;
18
    use FileProcessTrait;
19
    use GetOptionTrait;
20
21
    /**
22
     * Compress a file and return the new file
23
     *
24
     * @param FileNodeInterface $node
25
     * @param array             $options
26
     *
27
     * @return FileNodeInterface
28
     */
29 12
    public function compress(FileNodeInterface $node, array $options = [])
30
    {
31 12
        if (!($node instanceof LocalFile)) {
32 1
            throw new InvalidArgumentException("Node: $node should be a LocalFile");
33
        }
34
35 11
        $this->options = $options;
36 11
        $pathInfo = pathinfo($node->getPath());
37
38 11
        if (!$node->exists()) {
39 1
            throw new InvalidArgumentException("The file: $node does not exist");
40
        }
41
42 10
        $outputFile = $node->getClone()
43 10
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.gz')
44 10
                           ->setCompression(CompressionType::GZIP);
45
46 10
        $this->log(LogLevel::INFO, "Compressing file: {file} into {target} using {compression}", [
47 10
            'file'        => $node,
48 10
            'target'      => $outputFile,
49
            'compression' => CompressionType::GZIP,
50
        ]);
51
52 10
        $cmd = "gzip -c {$node->getPath()} > {$outputFile->getPath()}";
53
54 10
        return $this->processFile($node, $outputFile, $cmd, $this->getOption('keepOldFile', true));
55
    }
56
57
    /**
58
     * Decompress a file and return the decompressed file
59
     *
60
     * @param FileNodeInterface $node
61
     * @param array             $options
62
     *
63
     * @return FileNodeInterface
64
     */
65 8
    public function decompress(FileNodeInterface $node, array $options = [])
66
    {
67 8
        if (!($node instanceof LocalFile)) {
68 1
            throw new InvalidArgumentException("Node: $node should be a LocalFile");
69
        }
70
71 7
        $this->options = $options;
72 7
        $pathInfo = pathinfo($node->getPath());
73
74 7
        if (!$node->exists()) {
75 1
            throw new InvalidArgumentException("The file: $node does not exist");
76
        }
77
78 6
        $outputFile = $node->getClone()
79 6
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'])
80 6
                           ->setCompression(CompressionType::NONE);
81
82 6
        $this->log(LogLevel::INFO, "DeCompressing file: {file} into {target} using {compression}", [
83 6
            'file'        => $node,
84 6
            'target'      => $outputFile,
85
            'compression' => CompressionType::GZIP,
86
        ]);
87
88 6
        $cmd = "gunzip -c {$node->getPath()} > {$outputFile->getPath()}";
89
90 6
        return $this->processFile($node, $outputFile, $cmd, $this->getOption('keepOldFile', true));
91
    }
92
}
93