1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-file |
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-file/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-file |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Modify\Compress; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Helper\OptionalLoggerTrait; |
17
|
|
|
use Graze\DataFile\Helper\Process\ProcessFactoryAwareInterface; |
18
|
|
|
use Graze\DataFile\Helper\Process\ProcessTrait; |
19
|
|
|
use Graze\DataFile\Modify\FileModifierInterface; |
20
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
21
|
|
|
use Graze\DataFile\Node\LocalFileNodeInterface; |
22
|
|
|
use InvalidArgumentException; |
23
|
|
|
use Psr\Log\LoggerAwareInterface; |
24
|
|
|
use Psr\Log\LogLevel; |
25
|
|
|
|
26
|
|
|
class FindCompression implements ProcessFactoryAwareInterface, LoggerAwareInterface, FileModifierInterface |
27
|
|
|
{ |
28
|
|
|
use ProcessTrait; |
29
|
|
|
use OptionalLoggerTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var CompressionFactory |
33
|
|
|
*/ |
34
|
|
|
private $factory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* FindCompression constructor. |
38
|
|
|
* |
39
|
|
|
* @param CompressionFactory $factory |
40
|
|
|
*/ |
41
|
8 |
|
public function __construct(CompressionFactory $factory) |
42
|
|
|
{ |
43
|
8 |
|
$this->factory = $factory; |
44
|
8 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Find the compression of a file |
48
|
|
|
* |
49
|
|
|
* @param LocalFileNodeInterface $file |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
6 |
|
public function getCompression(LocalFileNodeInterface $file) |
54
|
|
|
{ |
55
|
6 |
|
$cmd = "file --brief --uncompress --mime {$file->getPath()}"; |
56
|
|
|
|
57
|
6 |
|
$process = $this->getProcess($cmd); |
58
|
6 |
|
$process->mustRun(); |
59
|
|
|
|
60
|
5 |
|
$result = $process->getOutput(); |
61
|
5 |
|
if (preg_match('/compressed-encoding=application\/(?:x-)?(.+?);/i', $result, $matches)) { |
62
|
4 |
|
if ($this->factory->isCompression($matches[1])) { |
63
|
3 |
|
$this->log(LogLevel::DEBUG, "Found the compression for '{file}' as '{compression}'", [ |
64
|
3 |
|
'file' => $file, |
65
|
3 |
|
'compression' => $matches[1], |
66
|
3 |
|
]); |
67
|
3 |
|
return $matches[1]; |
68
|
|
|
} |
69
|
1 |
|
return CompressionFactory::TYPE_UNKNOWN; |
70
|
|
|
} |
71
|
1 |
|
return CompressionFactory::TYPE_NONE; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Can this file be modified by this modifier |
76
|
|
|
* |
77
|
|
|
* @param FileNodeInterface $file |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
3 |
|
public function canModify(FileNodeInterface $file) |
82
|
|
|
{ |
83
|
3 |
|
return ($file instanceof LocalFileNodeInterface && |
84
|
3 |
|
$file->exists()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Modify the file |
89
|
|
|
* |
90
|
|
|
* @param FileNodeInterface $file |
91
|
|
|
* @param array $options |
92
|
|
|
* |
93
|
|
|
* @return FileNodeInterface |
94
|
|
|
*/ |
95
|
2 |
View Code Duplication |
public function modify(FileNodeInterface $file, array $options = []) |
|
|
|
|
96
|
|
|
{ |
97
|
2 |
|
if (!$this->canModify($file) || !($file instanceof LocalFileNodeInterface)) { |
98
|
1 |
|
throw new InvalidArgumentException("The supplied file: '$file' does not implement LocalFileNodeInterface'"); |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
$file->setCompression($this->getCompression($file)); |
102
|
|
|
|
103
|
1 |
|
return $file; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.