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

Gzip   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 4
dl 78
loc 78
ccs 30
cts 30
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B compress() 27 27 3
B decompress() 27 27 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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