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 |
|
|
|
|
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
|
|
|
|
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.