1 | <?php |
||
43 | class Simple |
||
44 | { |
||
45 | |||
46 | /** |
||
47 | * The actions unique serial. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $serial; |
||
52 | |||
53 | /** |
||
54 | * The system logger implementation. |
||
55 | * |
||
56 | * @var \Psr\Log\LoggerInterface |
||
57 | */ |
||
58 | protected $systemLogger; |
||
59 | |||
60 | /** |
||
61 | * The RegistryProcessor instance to handle running threads. |
||
62 | * |
||
63 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
64 | */ |
||
65 | protected $registryProcessor; |
||
66 | |||
67 | /** |
||
68 | * The processor to read/write the necessary import data. |
||
69 | * |
||
70 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
71 | */ |
||
72 | protected $importProcessor; |
||
73 | |||
74 | /** |
||
75 | * The system configuration. |
||
76 | * |
||
77 | * @var \TechDivision\Import\ConfigurationInterface |
||
78 | */ |
||
79 | protected $configuration; |
||
80 | |||
81 | /** |
||
82 | * Set's the unique serial for this import process. |
||
83 | * |
||
84 | * @param string $serial The unique serial |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function setSerial($serial) |
||
92 | |||
93 | /** |
||
94 | * Return's the unique serial for this import process. |
||
95 | * |
||
96 | * @return string The unique serial |
||
97 | */ |
||
98 | public function getSerial() |
||
102 | |||
103 | /** |
||
104 | * Set's the system logger. |
||
105 | * |
||
106 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | public function setSystemLogger(LoggerInterface $systemLogger) |
||
114 | |||
115 | /** |
||
116 | * Return's the system logger. |
||
117 | * |
||
118 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
119 | */ |
||
120 | public function getSystemLogger() |
||
124 | |||
125 | /** |
||
126 | * Sets's the RegistryProcessor instance to handle the running threads. |
||
127 | * |
||
128 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
129 | * |
||
130 | * @return void |
||
131 | */ |
||
132 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
136 | |||
137 | /** |
||
138 | * Return's the RegistryProcessor instance to handle the running threads. |
||
139 | * |
||
140 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
141 | */ |
||
142 | public function getRegistryProcessor() |
||
146 | |||
147 | /** |
||
148 | * Set's the import processor instance. |
||
149 | * |
||
150 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
151 | * |
||
152 | * @return void |
||
153 | */ |
||
154 | 1 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
|
158 | |||
159 | /** |
||
160 | * Return's the import processor instance. |
||
161 | * |
||
162 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
163 | */ |
||
164 | 1 | public function getImportProcessor() |
|
168 | |||
169 | /** |
||
170 | * Set's the system configuration. |
||
171 | * |
||
172 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | public function setConfiguration(ConfigurationInterface $configuration) |
||
180 | |||
181 | /** |
||
182 | * Return's the system configuration. |
||
183 | * |
||
184 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
185 | */ |
||
186 | public function getConfiguration() |
||
190 | |||
191 | /** |
||
192 | * Return's the prefix for the import files. |
||
193 | * |
||
194 | * @return string The prefix |
||
195 | */ |
||
196 | public function getPrefix() |
||
200 | |||
201 | /** |
||
202 | * Return's the source directory that has to be watched for new files. |
||
203 | * |
||
204 | * @return string The source directory |
||
205 | */ |
||
206 | public function getSourceDir() |
||
210 | |||
211 | /** |
||
212 | * Parse the temporary upload directory for new files to be imported. |
||
213 | * |
||
214 | * @return void |
||
215 | * @throws \Exception Is thrown, if the import process can't be finished successfully |
||
216 | */ |
||
217 | public function import() |
||
258 | |||
259 | /** |
||
260 | * This method start's the import process by initializing |
||
261 | * the status and appends it to the registry. |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | public function start() |
||
266 | { |
||
267 | |||
268 | // log a message that import has been started |
||
269 | $this->getSystemLogger()->info(sprintf('Now start import with serial %s', $this->getSerial())); |
||
270 | |||
271 | // initialize the status |
||
272 | $status = array(RegistryKeys::STATUS => 1); |
||
273 | |||
274 | // initialize the status information for the subjects */ |
||
275 | /** @var \TechDivision\Import\Configuration\SubjectInterface $subject */ |
||
276 | foreach ($this->getConfiguration()->getSubjects() as $subject) { |
||
277 | $status[$subject->getPrefix()] = array(); |
||
278 | } |
||
279 | |||
280 | // append it to the registry |
||
281 | $this->getRegistryProcessor()->setAttribute($this->getSerial(), $status); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Prepares the global data for the import process. |
||
286 | * |
||
287 | * @return void |
||
288 | */ |
||
289 | public function setUp() |
||
290 | { |
||
291 | |||
292 | // load the registry |
||
293 | $importProcessor = $this->getImportProcessor(); |
||
294 | $registryProcessor = $this->getRegistryProcessor(); |
||
295 | |||
296 | // initialize the array for the global data |
||
297 | $globalData = array(); |
||
298 | |||
299 | // initialize the global data |
||
300 | $globalData[RegistryKeys::STORES] = $importProcessor->getStores(); |
||
301 | $globalData[RegistryKeys::LINK_TYPES] = $importProcessor->getLinkTypes(); |
||
302 | $globalData[RegistryKeys::TAX_CLASSES] = $importProcessor->getTaxClasses(); |
||
303 | $globalData[RegistryKeys::DEFAULT_STORE] = $importProcessor->getDefaultStore(); |
||
304 | $globalData[RegistryKeys::STORE_WEBSITES] = $importProcessor->getStoreWebsites(); |
||
305 | $globalData[RegistryKeys::LINK_ATTRIBUTES] = $importProcessor->getLinkAttributes(); |
||
306 | $globalData[RegistryKeys::ROOT_CATEGORIES] = $importProcessor->getRootCategories(); |
||
307 | $globalData[RegistryKeys::CORE_CONFIG_DATA] = $importProcessor->getCoreConfigData(); |
||
308 | $globalData[RegistryKeys::ATTRIBUTE_SETS] = $eavAttributeSets = $importProcessor->getEavAttributeSetsByEntityTypeId(4); |
||
309 | |||
310 | // prepare the categories |
||
311 | $categories = array(); |
||
312 | foreach ($importProcessor->getCategories() as $category) { |
||
313 | // expload the entity IDs from the category path |
||
314 | $entityIds = explode('/', $category[MemberNames::PATH]); |
||
315 | |||
316 | // cut-off the root category |
||
317 | array_shift($entityIds); |
||
318 | |||
319 | // continue with the next category if no entity IDs are available |
||
320 | if (sizeof($entityIds) === 0) { |
||
321 | continue; |
||
322 | } |
||
323 | |||
324 | // initialize the array for the path elements |
||
325 | $path = array(); |
||
326 | foreach ($importProcessor->getCategoryVarcharsByEntityIds($entityIds) as $cat) { |
||
327 | $path[] = $cat[MemberNames::VALUE]; |
||
328 | } |
||
329 | |||
330 | // append the catogory with the string path as key |
||
331 | $categories[implode('/', $path)] = $category; |
||
332 | } |
||
333 | |||
334 | // initialize the array with the categories |
||
335 | $globalData[RegistryKeys::CATEGORIES] = $categories; |
||
336 | |||
337 | // prepare an array with the EAV attributes grouped by their attribute set name as keys |
||
338 | $eavAttributes = array(); |
||
339 | foreach (array_keys($eavAttributeSets) as $eavAttributeSetName) { |
||
340 | $eavAttributes[$eavAttributeSetName] = $importProcessor->getEavAttributesByEntityTypeIdAndAttributeSetName(4, $eavAttributeSetName); |
||
341 | } |
||
342 | |||
343 | // initialize the array with the EAV attributes |
||
344 | $globalData[RegistryKeys::EAV_ATTRIBUTES] = $eavAttributes; |
||
345 | |||
346 | // add the status with the global data |
||
347 | $registryProcessor->mergeAttributesRecursive( |
||
348 | $this->getSerial(), |
||
349 | array(RegistryKeys::GLOBAL_DATA => $globalData) |
||
350 | ); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Process all the subjects defined in the system configuration. |
||
355 | * |
||
356 | * @return void |
||
357 | * @throws \Exception Is thrown, if one of the subjects can't be processed |
||
358 | */ |
||
359 | public function processSubjects() |
||
360 | { |
||
361 | |||
362 | try { |
||
363 | // load system logger and registry |
||
364 | $systemLogger = $this->getSystemLogger(); |
||
365 | $importProcessor = $this->getImportProcessor(); |
||
366 | |||
367 | // load the subjects |
||
368 | $subjects = $this->getConfiguration()->getSubjects(); |
||
369 | |||
370 | // start the transaction |
||
371 | $importProcessor->getConnection()->beginTransaction(); |
||
372 | |||
373 | // process all the subjects found in the system configuration |
||
374 | foreach ($subjects as $subject) { |
||
375 | $this->processSubject($subject); |
||
376 | } |
||
377 | |||
378 | // commit the transaction |
||
379 | $importProcessor->getConnection()->commit(); |
||
380 | |||
381 | } catch (\Exception $e) { |
||
382 | // rollback the transaction |
||
383 | $importProcessor->getConnection()->rollBack(); |
||
384 | |||
385 | // re-throw the exception |
||
386 | throw $e; |
||
387 | } |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Process the subject with the passed name/identifier. |
||
392 | * |
||
393 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
394 | * |
||
395 | * @return void |
||
396 | * @throws \Exception Is thrown, if the subject can't be processed |
||
397 | */ |
||
398 | public function processSubject($subject) |
||
399 | { |
||
400 | |||
401 | // load the system logger |
||
402 | $systemLogger = $this->getSystemLogger(); |
||
403 | |||
404 | // init file iterator on deployment directory |
||
405 | $fileIterator = new \FilesystemIterator($sourceDir = $subject->getSourceDir()); |
||
406 | |||
407 | // clear the filecache |
||
408 | clearstatcache(); |
||
409 | |||
410 | // prepare the regex to find the files to be imported |
||
411 | $regex = sprintf('/^.*\/%s.*\\.csv$/', $subject->getPrefix()); |
||
412 | |||
413 | // log a debug message |
||
414 | $systemLogger->debug( |
||
415 | sprintf('Now checking directory %s for files with regex %s to import', $sourceDir, $regex) |
||
416 | ); |
||
417 | |||
418 | // iterate through all CSV files and start import process |
||
419 | foreach (new \RegexIterator($fileIterator, $regex) as $filename) { |
||
420 | try { |
||
421 | // prepare the flag filenames |
||
422 | $inProgressFilename = sprintf('%s.inProgress', $filename); |
||
423 | $importedFilename = sprintf('%s.imported', $filename); |
||
424 | $failedFilename = sprintf('%s.failed', $filename); |
||
425 | |||
426 | // query whether or not the file has already been imported |
||
427 | if (is_file($failedFilename) || |
||
428 | is_file($importedFilename) || |
||
429 | is_file($inProgressFilename) |
||
430 | ) { |
||
431 | // log a debug message |
||
432 | $systemLogger->debug( |
||
433 | sprintf('Import running, found inProgress file %s', $inProgressFilename) |
||
434 | ); |
||
435 | |||
436 | // ignore the file |
||
437 | continue; |
||
438 | } |
||
439 | |||
440 | // flag file as in progress |
||
441 | touch($inProgressFilename); |
||
442 | |||
443 | // process the subject |
||
444 | $this->subjectFactory($subject)->import($this->getSerial(), $filename->getPathname()); |
||
445 | |||
446 | // rename flag file, because import has been successfull |
||
447 | rename($inProgressFilename, $importedFilename); |
||
448 | |||
449 | } catch (\Exception $e) { |
||
450 | // rename the flag file, because import failed and write the stack trace |
||
451 | rename($inProgressFilename, $failedFilename); |
||
452 | file_put_contents($failedFilename, $e->__toString()); |
||
453 | |||
454 | // re-throw the exception |
||
455 | throw $e; |
||
456 | } |
||
457 | } |
||
458 | |||
459 | // and and log a message that the subject has been processed |
||
460 | $systemLogger->info(sprintf('Successfully processed subject %s!', $subject->getClassName())); |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Factory method to create new handler instances. |
||
465 | * |
||
466 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
467 | * |
||
468 | * @return object The handler instance |
||
469 | */ |
||
470 | public function subjectFactory($subject) |
||
499 | |||
500 | /** |
||
501 | * Lifecycle callback that will be inovked after the |
||
502 | * import process has been finished. |
||
503 | * |
||
504 | * @return void |
||
505 | */ |
||
506 | public function tearDown() |
||
510 | |||
511 | /** |
||
512 | * This method finishes the import process and cleans the registry. |
||
513 | * |
||
514 | * @return void |
||
515 | */ |
||
516 | public function finish() |
||
520 | } |
||
521 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.