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

Zip   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.12%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 4
dl 102
loc 102
ccs 32
cts 34
cp 0.9412
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compress() 8 8 2
A zip() 22 22 2
A decompress() 7 7 2
A unzip() 23 23 2

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 1
    public function compress(FileNodeInterface $node, array $options = [])
31
    {
32 1
        if (!($node instanceof LocalFile)) {
33 1
            throw new InvalidArgumentException("Node: $node should be a LocalFile");
34
        }
35
36
        return $this->zip($node, $options);
37
    }
38
39
    /**
40
     * @extend Graze\DataFile\Node\File\LocalFile
41
     *
42
     * @param LocalFile $file
43
     * @param array     $options -keepOldFile <bool> (Default: true)
44
     *
45
     * @return LocalFile
46
     */
47 7
    public function zip(LocalFile $file, array $options = [])
48
    {
49 7
        $this->options = $options;
50 7
        $pathInfo = pathinfo($file->getPath());
51
52 7
        if (!$file->exists()) {
53 1
            throw new InvalidArgumentException("The file: $file does not exist");
54
        }
55
56 6
        $outputFile = $file->getClone()
57 6
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.zip')
58 6
                           ->setCompression(CompressionType::ZIP);
59
60 6
        $this->log(LogLevel::INFO, "Compressing file: {file} into {target} using {compression}", [
61 6
            'file'        => $file,
62 6
            'target'      => $outputFile,
63
            'compression' => CompressionType::ZIP,
64
        ]);
65 6
        $cmd = "zip {$outputFile->getPath()} {$file->getPath()}";
66
67 6
        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->unzip($node, $options);
84
    }
85
86
    /**
87
     * @extend Graze\DataFile\Node\File\LocalFile
88
     *
89
     * @param LocalFile $file
90
     * @param array     $options
91
     *
92
     * @return FileNodeInterface
93
     */
94 5
    public function unzip(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::ZIP,
111
        ]);
112
113 4
        $cmd = "unzip -p {$file->getPath()} > {$outputFile->getPath()}";
114
115 4
        return $this->processFile($file, $outputFile, $cmd, $this->getOption('keepOldFile', true));
116
    }
117
}
118