Completed
Push — master ( a15851...522585 )
by Harry
02:42
created

AbstractCompressor::decompress()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
ccs 17
cts 17
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
abstract class AbstractCompressor implements CompressorInterface, DeCompressorInterface, LoggerAwareInterface, ProcessFactoryAwareInterface
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 20
    public function compress(FileNodeInterface $node, array $options = [])
30
    {
31 20
        if (!($node instanceof LocalFile)) {
32 2
            throw new InvalidArgumentException("Node: $node should be a LocalFile");
33
        }
34
35 18
        $this->options = $options;
36 18
        $pathInfo = pathinfo($node->getPath());
37
38 18
        if (!$node->exists()) {
39 2
            throw new InvalidArgumentException("The file: $node does not exist");
40
        }
41
42 16
        $outputFile = $node->getClone()
43 16
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.' . $this->getExtension())
44 16
                           ->setCompression($this->getCompression());
45
46 16
        $this->log(LogLevel::INFO, "Compressing file: {file} into {target} using {compression}", [
47 16
            'file'        => $node,
48 16
            'target'      => $outputFile,
49 16
            'compression' => $this->getCompression(),
50 16
        ]);
51
52 16
        $cmd = $this->getCompressCommand($node->getPath(), $outputFile->getPath());
53
54 16
        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 18
    public function decompress(FileNodeInterface $node, array $options = [])
66
    {
67 14
        if (!($node instanceof LocalFile)) {
68 2
            throw new InvalidArgumentException("Node: $node should be a LocalFile");
69
        }
70
71 12
        $this->options = $options;
72 12
        $pathInfo = pathinfo($node->getPath());
73
74 12
        if (!$node->exists()) {
75 2
            throw new InvalidArgumentException("The file: $node does not exist");
76
        }
77
78 10
        $outputFile = $node->getClone()
79 18
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'])
80 10
                           ->setCompression(CompressionType::NONE);
81
82 10
        $this->log(LogLevel::INFO, "DeCompressing file: {file} into {target} using {compression}", [
83 10
            'file'        => $node,
84 10
            'target'      => $outputFile,
85 10
            'compression' => $this->getCompression(),
86 10
        ]);
87
88 10
        $cmd = $this->getDecompressCommand($node->getPath(), $outputFile->getPath());
89
90 10
        return $this->processFile($node, $outputFile, $cmd, $this->getOption('keepOldFile', true));
91
    }
92
93
    /**
94
     * Get the extension used by this compressor
95
     *
96
     * @return string
97
     */
98
    abstract protected function getExtension();
99
100
    /**
101
     * @return string
102
     */
103
    abstract protected function getCompression();
104
105
    /**
106
     * Get the command line to compress a file
107
     *
108
     * @param string $fromPath
109
     * @param string $toPath
110
     *
111
     * @return string
112
     */
113
    abstract protected function getCompressCommand($fromPath, $toPath);
114
115
    /**
116
     * Get the command line to decompress a file
117
     *
118
     * @param string $fromPath
119
     * @param string $toPath
120
     *
121
     * @return string
122
     */
123
    abstract protected function getDecompressCommand($fromPath, $toPath);
124
}
125