|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Plugins\ArchivePlugin |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Plugins; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\RegistryKeys; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Plugin that archives the artefacts. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Tim Wagner <[email protected]> |
|
29
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
31
|
|
|
* @link https://github.com/techdivision/import |
|
32
|
|
|
* @link http://www.techdivision.com |
|
33
|
|
|
*/ |
|
34
|
|
|
class ArchivePlugin extends AbstractPlugin |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Process the plugin functionality. |
|
39
|
|
|
* |
|
40
|
|
|
* @return void |
|
41
|
|
|
* @throws \Exception Is thrown, if the plugin can not be processed |
|
42
|
|
|
*/ |
|
43
|
|
|
public function process() |
|
44
|
|
|
{ |
|
45
|
|
|
|
|
46
|
|
|
// query whether or not, the import artefacts have to be archived |
|
47
|
|
|
if (!$this->getConfiguration()->haveArchiveArtefacts()) { |
|
48
|
|
|
$this->getSystemLogger()->info('Archiving functionality has not been activated'); |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// load the actual status |
|
53
|
|
|
$status = $this->getRegistryProcessor()->getAttribute($this->getSerial()); |
|
54
|
|
|
|
|
55
|
|
|
// load the number of imported bunches from the status |
|
56
|
|
|
$bunches = $status[RegistryKeys::BUNCHES]; |
|
57
|
|
|
|
|
58
|
|
|
// if no files have been imported, return immediately |
|
59
|
|
|
if ($bunches === 0) { |
|
60
|
|
|
$this->getSystemLogger()->info('Found no files to archive'); |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// clear the filecache |
|
65
|
|
|
clearstatcache(); |
|
66
|
|
|
|
|
67
|
|
|
// query whether or not the configured source directory is available |
|
68
|
|
View Code Duplication |
if (!is_dir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) { |
|
|
|
|
|
|
69
|
|
|
throw new \Exception(sprintf('Configured source directory %s is not available!', $sourceDir)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// init file iterator on source directory |
|
73
|
|
|
$fileIterator = new \FilesystemIterator($sourceDir); |
|
74
|
|
|
|
|
75
|
|
|
// log the number of files that has to be archived |
|
76
|
|
|
$this->getSystemLogger()->info(sprintf('Found %d files to archive in directory %s', $bunches, $sourceDir)); |
|
77
|
|
|
|
|
78
|
|
|
// initialize the directory to create the archive in |
|
79
|
|
|
$archiveDir = sprintf('%s/%s', $this->getConfiguration()->getTargetDir(), $this->getConfiguration()->getArchiveDir()); |
|
80
|
|
|
|
|
81
|
|
|
// query whether or not the directory already exists |
|
82
|
|
|
if (!is_dir($archiveDir)) { |
|
83
|
|
|
mkdir($archiveDir); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// create the ZIP archive |
|
87
|
|
|
$archive = new \ZipArchive(); |
|
88
|
|
|
$archive->open($archiveFile = sprintf('%s/%s.zip', $archiveDir, $this->getSerial()), \ZipArchive::CREATE); |
|
89
|
|
|
|
|
90
|
|
|
// iterate through all files and add them to the ZIP archive |
|
91
|
|
|
foreach ($fileIterator as $filename) { |
|
92
|
|
|
$archive->addFile($filename); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// save the ZIP archive |
|
96
|
|
|
$archive->close(); |
|
97
|
|
|
|
|
98
|
|
|
// finally remove the directory with the imported files |
|
99
|
|
|
$this->removeDir($sourceDir); |
|
100
|
|
|
|
|
101
|
|
|
// and and log a message that the import artefacts have been archived |
|
102
|
|
|
$this->getSystemLogger()->info(sprintf('Successfully archived imported files to %s!', $archiveFile)); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
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.