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

Zip   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
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 77
loc 77
ccs 30
cts 30
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B compress() 26 26 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
use Symfony\Component\Process\Exception\ProcessFailedException;
15
16 View Code Duplication
class Zip 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...
17
{
18
    use OptionalLoggerTrait;
19
    use FileProcessTrait;
20
    use GetOptionTrait;
21
22
    /**
23
     * Compress a file and return the new file
24
     *
25
     * @param FileNodeInterface $node
26
     * @param array             $options
27
     *
28
     * @return FileNodeInterface
29
     */
30 8
    public function compress(FileNodeInterface $node, array $options = [])
31
    {
32 8
        if (!($node instanceof LocalFile)) {
33 1
            throw new InvalidArgumentException("Node: $node should be a LocalFile");
34
        }
35
36 7
        $this->options = $options;
37 7
        $pathInfo = pathinfo($node->getPath());
38
39 7
        if (!$node->exists()) {
40 1
            throw new InvalidArgumentException("The file: $node does not exist");
41
        }
42
43 6
        $outputFile = $node->getClone()
44 6
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.zip')
45 6
                           ->setCompression(CompressionType::ZIP);
46
47 6
        $this->log(LogLevel::INFO, "Compressing file: {file} into {target} using {compression}", [
48 6
            'file'        => $node,
49 6
            'target'      => $outputFile,
50
            'compression' => CompressionType::ZIP,
51
        ]);
52 6
        $cmd = "zip {$outputFile->getPath()} {$node->getPath()}";
53
54 6
        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 6
    public function decompress(FileNodeInterface $node, array $options = [])
66
    {
67 6
        if (!($node instanceof LocalFile)) {
68 1
            throw new InvalidArgumentException("Node: $node should be a LocalFile");
69
        }
70
71 5
        $this->options = $options;
72 5
        $pathInfo = pathinfo($node->getPath());
73
74 5
        if (!$node->exists()) {
75 1
            throw new InvalidArgumentException("The file: $node does not exist");
76
        }
77
78 4
        $outputFile = $node->getClone()
79 4
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'])
80 4
                           ->setCompression(CompressionType::NONE);
81
82 4
        $this->log(LogLevel::INFO, "DeCompressing file: {file} into {target} using {compression}", [
83 4
            'file'        => $node,
84 4
            'target'      => $outputFile,
85
            'compression' => CompressionType::ZIP,
86
        ]);
87
88 4
        $cmd = "unzip -p {$node->getPath()} > {$outputFile->getPath()}";
89
90 4
        return $this->processFile($node, $outputFile, $cmd, $this->getOption('keepOldFile', true));
91
    }
92
}
93