These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * TechDivision\Import\Listeners\ArchiveListener |
||
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 2019 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\Listeners; |
||
22 | |||
23 | use League\Event\EventInterface; |
||
24 | use League\Event\AbstractListener; |
||
25 | use Doctrine\Common\Collections\Collection; |
||
26 | use TechDivision\Import\SystemLoggerTrait; |
||
27 | use TechDivision\Import\Utils\RegistryKeys; |
||
28 | use TechDivision\Import\ApplicationInterface; |
||
29 | use TechDivision\Import\Configuration\ConfigurationInterface; |
||
30 | use TechDivision\Import\Services\ImportProcessorInterface; |
||
31 | use TechDivision\Import\Services\RegistryProcessorInterface; |
||
32 | |||
33 | /** |
||
34 | * Listener that archives the import artefacts. |
||
35 | * |
||
36 | * @author Tim Wagner <[email protected]> |
||
37 | * @copyright 2019 TechDivision GmbH <[email protected]> |
||
38 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
||
39 | * @link https://github.com/techdivision/import |
||
40 | * @link http://www.techdivision.com |
||
41 | */ |
||
42 | class ArchiveListener extends AbstractListener |
||
43 | { |
||
44 | |||
45 | /** |
||
46 | * The trait that provides basic system logger functionality. |
||
47 | * |
||
48 | * @var \TechDivision\Import\SystemLoggerTrait |
||
49 | */ |
||
50 | use SystemLoggerTrait; |
||
51 | |||
52 | /** |
||
53 | * The import processor instance. |
||
54 | * |
||
55 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
56 | */ |
||
57 | protected $registryProcessor; |
||
58 | |||
59 | /** |
||
60 | * The registry processor instance. |
||
61 | * |
||
62 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
63 | */ |
||
64 | protected $importProcessor; |
||
65 | |||
66 | /** |
||
67 | * The configuration instance. |
||
68 | * |
||
69 | * @var \TechDivision\Import\Configuration\ConfigurationInterface |
||
70 | */ |
||
71 | protected $configuration; |
||
72 | |||
73 | /** |
||
74 | * Initializes the plugin with the application instance. |
||
75 | * |
||
76 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
77 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
78 | * @param \Doctrine\Common\Collections\Collection $systemLoggers The array with the system loggers instances |
||
79 | * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance |
||
80 | */ |
||
81 | View Code Duplication | public function __construct( |
|
82 | RegistryProcessorInterface $registryProcessor, |
||
83 | ImportProcessorInterface $importProcessor, |
||
84 | Collection $systemLoggers, |
||
85 | ConfigurationInterface $configuration |
||
86 | ) { |
||
87 | |||
88 | // set the passed instances |
||
89 | $this->registryProcessor = $registryProcessor; |
||
90 | $this->importProcessor = $importProcessor; |
||
91 | $this->systemLoggers = $systemLoggers; |
||
92 | $this->configuration = $configuration; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Return's the registry processor instance. |
||
97 | * |
||
98 | * @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance |
||
99 | */ |
||
100 | protected function getRegistryProcessor() |
||
101 | { |
||
102 | return $this->registryProcessor; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Return's the import processor instance. |
||
107 | * |
||
108 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
109 | */ |
||
110 | protected function getImportProcessor() |
||
111 | { |
||
112 | return $this->importProcessor; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Return's the configuration instance. |
||
117 | * |
||
118 | * @return \TechDivision\Import\Configuration\ConfigurationInterface The configuration instance |
||
119 | */ |
||
120 | protected function getConfiguration() |
||
121 | { |
||
122 | return $this->configuration; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Handle the event. |
||
127 | * |
||
128 | * @param \League\Event\EventInterface $event The event that triggered the listener |
||
129 | * @param \TechDivision\Import\ApplicationInterface $application The application instance |
||
130 | * |
||
131 | * @return void |
||
132 | */ |
||
133 | public function handle(EventInterface $event, ApplicationInterface $application = null) |
||
134 | { |
||
135 | |||
136 | // query whether or not, the import artefacts have to be archived |
||
137 | if (!$this->getConfiguration()->haveArchiveArtefacts()) { |
||
138 | $this->getSystemLogger()->info('Archiving functionality has not been activated'); |
||
139 | return; |
||
140 | } |
||
141 | |||
142 | // load the actual status |
||
143 | $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
||
144 | |||
145 | // load the number of imported bunches from the status |
||
146 | $bunches = $status[RegistryKeys::BUNCHES]; |
||
147 | |||
148 | // if no files have been imported, return immediately |
||
149 | if ($bunches === 0) { |
||
150 | $this->getSystemLogger()->info('Found no files to archive'); |
||
151 | return; |
||
152 | } |
||
153 | |||
154 | // clear the filecache |
||
155 | clearstatcache(); |
||
156 | |||
157 | // query whether or not the configured source directory is available |
||
158 | View Code Duplication | if (!is_dir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) { |
|
0 ignored issues
–
show
|
|||
159 | throw new \Exception(sprintf('Configured source directory %s is not available!', $sourceDir)); |
||
160 | } |
||
161 | |||
162 | // init file iterator on source directory |
||
163 | $fileIterator = new \FilesystemIterator($sourceDir); |
||
164 | |||
165 | // log the number of files that has to be archived |
||
166 | $this->getSystemLogger()->info(sprintf('Found %d files to archive in directory %s', $bunches, $sourceDir)); |
||
167 | |||
168 | // try to load the archive directory |
||
169 | $archiveDir = $this->getConfiguration()->getArchiveDir(); |
||
170 | |||
171 | // query whether or not the specified archive directory already exists |
||
172 | if ($archiveDir === null) { |
||
173 | // try to initialize a default archive directory by concatenating 'archive' to the target directory |
||
174 | $archiveDir = sprintf('var/import_history'); |
||
175 | } |
||
176 | |||
177 | // query whether or not the archive directory already exists |
||
178 | if (!is_dir($archiveDir)) { |
||
179 | // create the archive directory if possible |
||
180 | if (mkdir($archiveDir, 0755, true)) { |
||
181 | $this->getSystemLogger()->info(sprintf('Successfully created archived archive directory %s', $archiveDir)); |
||
182 | } else { |
||
183 | // initialize the message that the archive directory can not be created |
||
184 | $message = sprintf('Can\'t create archive directory %s', $archiveDir); |
||
185 | |||
186 | // log a message if we're in debug mode, else throw an exception |
||
187 | if ($this->getConfiguration()->isDebugMode()) { |
||
188 | $this->getSystemLogger()->error($message); |
||
189 | } else { |
||
190 | throw new \Exception($message); |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | |||
195 | // create the ZIP archive |
||
196 | $archive = new \ZipArchive(); |
||
197 | $archive->open($archiveFile = sprintf('%s/%s.zip', $archiveDir, $application->getSerial()), \ZipArchive::CREATE); |
||
198 | |||
199 | // iterate through all files and add them to the ZIP archive |
||
200 | /** @var \SplFileInfo $filename */ |
||
201 | foreach ($fileIterator as $filename) { |
||
202 | if ($filename->isFile()) { |
||
203 | $archive->addFile($filename, basename($filename)); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | // save the ZIP archive |
||
208 | $archive->close(); |
||
209 | |||
210 | // append the name of the archive file in the registry |
||
211 | $this->getRegistryProcessor()->mergeAttributesRecursive(RegistryKeys::STATUS, array(RegistryKeys::ARCHIVE_FILE => $archiveFile)); |
||
212 | |||
213 | // and and log a message that the import artefacts have been archived |
||
214 | $this->getSystemLogger()->info(sprintf('Successfully archived imported files to %s!', basename($archiveFile))); |
||
215 | } |
||
216 | } |
||
217 |
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.