1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataFile\Modify\Contract; |
4
|
|
|
|
5
|
|
|
use DirectoryIterator; |
6
|
|
|
use Graze\DataFile\Helper\GetOptionTrait; |
7
|
|
|
use Graze\DataFile\Helper\OptionalLoggerTrait; |
8
|
|
|
use Graze\DataFile\Helper\Process\ProcessFactoryAwareInterface; |
9
|
|
|
use Graze\DataFile\Helper\Process\ProcessTrait; |
10
|
|
|
use Graze\DataFile\Modify\Compress\CompressionType; |
11
|
|
|
use Graze\DataFile\Modify\MakeDirectory; |
12
|
|
|
use Graze\DataFile\Node\FileNodeCollectionInterface; |
13
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
14
|
|
|
use Graze\DataFile\Node\LocalFile; |
15
|
|
|
use Psr\Log\LoggerAwareInterface; |
16
|
|
|
use Psr\Log\LogLevel; |
17
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
18
|
|
|
|
19
|
|
|
class MergeFiles implements FileContractorInterface, LoggerAwareInterface, ProcessFactoryAwareInterface |
20
|
|
|
{ |
21
|
|
|
use GetOptionTrait; |
22
|
|
|
use OptionalLoggerTrait; |
23
|
|
|
use ProcessTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param FileNodeCollectionInterface $files |
27
|
|
|
* |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
9 |
|
public function canContract(FileNodeCollectionInterface $files) |
31
|
|
|
{ |
32
|
9 |
|
foreach ($files->getIterator() as $file) { |
33
|
8 |
|
if (!($file instanceof LocalFile) || |
34
|
8 |
|
!($file->exists()) || |
35
|
8 |
|
($file->getCompression() != CompressionType::NONE) |
36
|
|
|
) { |
37
|
8 |
|
return false; |
38
|
|
|
} |
39
|
|
|
} |
40
|
9 |
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Do the expansion and return a collection |
45
|
|
|
* |
46
|
|
|
* @param FileNodeCollectionInterface $files |
47
|
|
|
* @param FileNodeInterface $file |
48
|
|
|
* @param array $options :keepOldFiles <bool> (Default: true) Keep the old files after merging |
49
|
|
|
* |
50
|
|
|
* @return FileNodeInterface |
51
|
|
|
*/ |
52
|
5 |
|
public function contract( |
53
|
|
|
FileNodeCollectionInterface $files, |
54
|
|
|
FileNodeInterface $file, |
55
|
|
|
array $options = [] |
56
|
|
|
) { |
57
|
5 |
|
$this->options = $options; |
58
|
5 |
|
if (!$this->canContract($files)) { |
59
|
|
|
throw new \InvalidArgumentException("The supplied files are not valid"); |
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
$this->log(LogLevel::INFO, "Merging files in collection $files into: {$file}"); |
63
|
|
|
|
64
|
|
|
$filePaths = $files->map(function (LocalFile $item) { |
65
|
5 |
|
return $item->getPath(); |
66
|
5 |
|
}); |
67
|
|
|
|
68
|
5 |
|
$cmd = sprintf( |
69
|
5 |
|
'cat %s > %s', |
70
|
5 |
|
implode(' ', $filePaths), |
71
|
5 |
|
$file->getPath() |
72
|
|
|
); |
73
|
|
|
|
74
|
5 |
|
$maker = new MakeDirectory(); |
75
|
5 |
|
$maker->makeDirectory($file); |
|
|
|
|
76
|
|
|
|
77
|
5 |
|
$process = $this->getProcess($cmd); |
78
|
5 |
|
$process->run(); |
79
|
|
|
|
80
|
5 |
|
if (!$process->isSuccessful()) { |
81
|
1 |
|
throw new ProcessFailedException($process); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
if (!$this->getOption('keepOldFiles', true)) { |
85
|
2 |
|
$this->log(LogLevel::DEBUG, "Deleting old files in collection $files"); |
86
|
2 |
|
$files->map(function (LocalFile $item) { |
87
|
2 |
|
if ($item->exists()) { |
88
|
2 |
|
$item->delete(); |
89
|
|
|
} |
90
|
2 |
|
$count = iterator_count(new DirectoryIterator($item->getDirectory())); |
91
|
2 |
|
if ($count == 2) { |
92
|
2 |
|
rmdir($item->getDirectory()); |
93
|
|
|
} |
94
|
2 |
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
4 |
|
return $file; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.