1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-flow |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-flow/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-flow |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFlow\Flow\File\Compression; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Helper\OptionalLoggerTrait; |
17
|
|
|
use Graze\DataFile\Modify\Compress\CompressionFactory; |
18
|
|
|
use Graze\DataFile\Modify\Exception\InvalidCompressionTypeException; |
19
|
|
|
use Graze\DataFile\Node\LocalFile; |
20
|
|
|
use Graze\DataFlow\Flow\InvokeTrait; |
21
|
|
|
use Graze\DataFlow\FlowInterface; |
22
|
|
|
use Graze\DataNode\NodeInterface; |
23
|
|
|
use InvalidArgumentException; |
24
|
|
|
use Psr\Log\LoggerAwareInterface; |
25
|
|
|
use Psr\Log\LogLevel; |
26
|
|
|
|
27
|
|
|
class Compress implements FlowInterface, LoggerAwareInterface |
28
|
|
|
{ |
29
|
|
|
use InvokeTrait; |
30
|
|
|
use OptionalLoggerTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $compression; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $options; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Compress constructor. |
44
|
|
|
* |
45
|
|
|
* @param string $compression |
46
|
|
|
* @param array $options |
47
|
|
|
*/ |
48
|
13 |
|
public function __construct($compression, array $options = []) |
49
|
|
|
{ |
50
|
13 |
|
$this->compression = $compression; |
51
|
13 |
|
$this->options = $options; |
52
|
13 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param NodeInterface $node |
56
|
|
|
* |
57
|
|
|
* @return LocalFile |
58
|
|
|
* @throws InvalidCompressionTypeException |
59
|
|
|
* |
60
|
|
|
*/ |
61
|
13 |
|
public function flow(NodeInterface $node) |
62
|
|
|
{ |
63
|
13 |
|
if (!($node instanceof LocalFile)) { |
64
|
1 |
|
throw new InvalidArgumentException("Node: $node should be an instance of LocalFile"); |
65
|
|
|
} |
66
|
|
|
|
67
|
12 |
|
$factory = new CompressionFactory(); |
68
|
|
|
|
69
|
12 |
|
$compressor = $factory->getCompressor($this->compression); |
70
|
12 |
|
$this->log(LogLevel::INFO, "Compressing file: '{file}' using '{compression}'", [ |
71
|
12 |
|
'file' => $node, |
72
|
12 |
|
'compression' => $this->compression, |
73
|
12 |
|
]); |
74
|
12 |
|
return $compressor->compress($node, $this->options); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|