|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Graze\DataFile\Modify\Compress; |
|
4
|
|
|
|
|
5
|
|
|
use Graze\DataFile\Node\LocalFileNodeInterface; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Psr\Log\LogLevel; |
|
8
|
|
|
|
|
9
|
|
|
trait CompressorTrait |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Compress a file and return the new file |
|
13
|
|
|
* |
|
14
|
|
|
* @param LocalFileNodeInterface $node |
|
15
|
|
|
* @param array $options |
|
16
|
|
|
* |
|
17
|
|
|
* @return LocalFileNodeInterface |
|
18
|
|
|
*/ |
|
19
|
18 |
|
public function compress(LocalFileNodeInterface $node, array $options = []) |
|
20
|
|
|
{ |
|
21
|
18 |
|
$pathInfo = pathinfo($node->getPath()); |
|
22
|
|
|
|
|
23
|
18 |
|
if (!$node->exists()) { |
|
24
|
2 |
|
throw new InvalidArgumentException("The file: $node does not exist"); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
16 |
|
$outputFile = $node->getClone() |
|
28
|
16 |
|
->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.' . $this->getExtension()) |
|
29
|
16 |
|
->setCompression($this->getName()); |
|
30
|
|
|
|
|
31
|
16 |
|
$this->log(LogLevel::INFO, "Compressing file: {file} into {target} using {compression}", [ |
|
32
|
16 |
|
'file' => $node, |
|
33
|
16 |
|
'target' => $outputFile, |
|
34
|
16 |
|
'compression' => $this->getName(), |
|
35
|
16 |
|
]); |
|
36
|
|
|
|
|
37
|
16 |
|
$cmd = $this->getCompressCommand($node, $outputFile); |
|
38
|
|
|
|
|
39
|
16 |
|
$keepOld = (isset($options['keepOldFile'])) ? $options['keepOldFile'] : true; |
|
40
|
|
|
|
|
41
|
16 |
|
return $this->processFile($node, $outputFile, $cmd, $keepOld); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Abstract Log function that might should be handed by the OptionalLoggerTrait or similar |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $level |
|
48
|
|
|
* @param string $message |
|
49
|
|
|
* @param array $context |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
abstract protected function log($level, $message, array $context = []); |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param LocalFileNodeInterface $node |
|
57
|
|
|
* @param LocalFileNodeInterface $outputFile |
|
58
|
|
|
* @param string $cmd |
|
59
|
|
|
* @param bool $keepOld |
|
60
|
|
|
* |
|
61
|
|
|
* @return LocalFileNodeInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
abstract protected function processFile( |
|
64
|
|
|
LocalFileNodeInterface $node, |
|
65
|
|
|
LocalFileNodeInterface $outputFile, |
|
66
|
|
|
$cmd, |
|
67
|
|
|
$keepOld = true |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
abstract public function getExtension(); |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
abstract public function getName(); |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get the command line to compress a file |
|
82
|
|
|
* |
|
83
|
|
|
* @param LocalFileNodeInterface $from |
|
84
|
|
|
* @param LocalFileNodeInterface $to |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
abstract public function getCompressCommand(LocalFileNodeInterface $from, LocalFileNodeInterface $to); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.