Code Duplication    Length = 102-102 lines in 2 locations

src/Modify/Compress/Gzip.php 1 location

@@ 17-118 (lines=102) @@
14
use Psr\Log\LogLevel;
15
use Symfony\Component\Process\Exception\ProcessFailedException;
16
17
class Gzip implements CompressorInterface, DeCompressorInterface, LoggerAwareInterface, ProcessFactoryAwareInterface
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
    public function compress(FileNodeInterface $node, array $options = [])
32
    {
33
        if (!($node instanceof LocalFile)) {
34
            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
    public function gzip(LocalFile $file, array $options = [])
47
    {
48
        $this->options = $options;
49
        $pathInfo = pathinfo($file->getPath());
50
51
        if (!$file->exists()) {
52
            throw new InvalidArgumentException("The file: $file does not exist");
53
        }
54
55
        $outputFile = $file->getClone()
56
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.gz')
57
                           ->setCompression(CompressionType::GZIP);
58
59
        $this->log(LogLevel::INFO, "Compressing file: {file} into {target} using {compression}", [
60
            'file'        => $file,
61
            'target'      => $outputFile,
62
            'compression' => CompressionType::GZIP,
63
        ]);
64
65
        $cmd = "gzip -c {$file->getPath()} > {$outputFile->getPath()}";
66
67
        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
    public function decompress(FileNodeInterface $node, array $options = [])
79
    {
80
        if (!($node instanceof LocalFile)) {
81
            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
    public function gunzip(LocalFile $file, array $options = [])
95
    {
96
        $this->options = $options;
97
        $pathInfo = pathinfo($file->getPath());
98
99
        if (!$file->exists()) {
100
            throw new InvalidArgumentException("The file: $file does not exist");
101
        }
102
103
        $outputFile = $file->getClone()
104
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'])
105
                           ->setCompression(CompressionType::NONE);
106
107
        $this->log(LogLevel::INFO, "DeCompressing file: {file} into {target} using {compression}", [
108
            'file'        => $file,
109
            'target'      => $outputFile,
110
            'compression' => CompressionType::GZIP,
111
        ]);
112
113
        $cmd = "gunzip -c {$file->getPath()} > {$outputFile->getPath()}";
114
115
116
        return $this->processFile($file, $outputFile, $cmd, $this->getOption('keepOldFile', true));
117
    }
118
}
119

src/Modify/Compress/Zip.php 1 location

@@ 16-117 (lines=102) @@
13
use Psr\Log\LogLevel;
14
use Symfony\Component\Process\Exception\ProcessFailedException;
15
16
class Zip implements CompressorInterface, DeCompressorInterface, LoggerAwareInterface, ProcessFactoryAwareInterface
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
    public function compress(FileNodeInterface $node, array $options = [])
31
    {
32
        if (!($node instanceof LocalFile)) {
33
            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
    public function zip(LocalFile $file, array $options = [])
48
    {
49
        $this->options = $options;
50
        $pathInfo = pathinfo($file->getPath());
51
52
        if (!$file->exists()) {
53
            throw new InvalidArgumentException("The file: $file does not exist");
54
        }
55
56
        $outputFile = $file->getClone()
57
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.zip')
58
                           ->setCompression(CompressionType::ZIP);
59
60
        $this->log(LogLevel::INFO, "Compressing file: {file} into {target} using {compression}", [
61
            'file'        => $file,
62
            'target'      => $outputFile,
63
            'compression' => CompressionType::ZIP,
64
        ]);
65
        $cmd = "zip {$outputFile->getPath()} {$file->getPath()}";
66
67
        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
    public function decompress(FileNodeInterface $node, array $options = [])
79
    {
80
        if (!($node instanceof LocalFile)) {
81
            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
    public function unzip(LocalFile $file, array $options = [])
95
    {
96
        $this->options = $options;
97
        $pathInfo = pathinfo($file->getPath());
98
99
        if (!$file->exists()) {
100
            throw new InvalidArgumentException("The file: $file does not exist");
101
        }
102
103
        $outputFile = $file->getClone()
104
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'])
105
                           ->setCompression(CompressionType::NONE);
106
107
        $this->log(LogLevel::INFO, "DeCompressing file: {file} into {target} using {compression}", [
108
            'file'        => $file,
109
            'target'      => $outputFile,
110
            'compression' => CompressionType::ZIP,
111
        ]);
112
113
        $cmd = "unzip -p {$file->getPath()} > {$outputFile->getPath()}";
114
115
        return $this->processFile($file, $outputFile, $cmd, $this->getOption('keepOldFile', true));
116
    }
117
}
118