Completed
Push — master ( cdcfa5...5f41e8 )
by Tim
02:42
created

Simple::processSubjects()   B

Complexity

Conditions 4
Paths 15

Size

Total Lines 47
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 47
ccs 0
cts 17
cp 0
rs 8.6845
cc 4
eloc 17
nc 15
nop 0
crap 20
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Simple
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-cli-simple
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Cli;
22
23
use Rhumsaa\Uuid\Uuid;
24
use Monolog\Logger;
25
use Psr\Log\LogLevel;
26
use Psr\Log\LoggerInterface;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Symfony\Component\Console\Helper\FormatterHelper;
30
use TechDivision\Import\Utils\RegistryKeys;
31
use TechDivision\Import\ApplicationInterface;
32
use TechDivision\Import\ConfigurationInterface;
33
use TechDivision\Import\Configuration\PluginConfigurationInterface;
34
use TechDivision\Import\Services\ImportProcessorInterface;
35
use TechDivision\Import\Services\RegistryProcessorInterface;
36
use TechDivision\Import\Cli\Exceptions\LineNotFoundException;
37
use TechDivision\Import\Cli\Exceptions\FileNotFoundException;
38
39
/**
40
 * The M2IF - Console Tool implementation.
41
 *
42
 * This is a example console tool implementation that should give developers an impression
43
 * on how the M2IF could be used to implement their own Magento 2 importer.
44
 *
45
 * @author    Tim Wagner <[email protected]>
46
 * @copyright 2016 TechDivision GmbH <[email protected]>
47
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
48
 * @link      https://github.com/techdivision/import-cli-simple
49
 * @link      http://www.techdivision.com
50
 */
51
class Simple implements ApplicationInterface
52
{
53
54
    /**
55
     * The default style to write messages to the symfony console.
56
     *
57
     * @var string
58
     */
59
    const DEFAULT_STYLE = 'info';
60
61
    /**
62
     * The TechDivision company name as ANSI art.
63
     *
64
     * @var string
65
     */
66
    protected $ansiArt = ' _______        _     _____  _       _     _
67
|__   __|      | |   |  __ \(_)     (_)   (_)
68
   | | ___  ___| |__ | |  | |___   ___ ___ _  ___  _ __
69
   | |/ _ \/ __| \'_ \| |  | | \ \ / / / __| |/ _ \| \'_ \
70
   | |  __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | |
71
   |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_|
72
';
73
74
    /**
75
     * The log level => console style mapping.
76
     *
77
     * @var array
78
     */
79
    protected $logLevelStyleMapping = array(
80
        LogLevel::INFO      => 'info',
81
        LogLevel::DEBUG     => 'comment',
82
        LogLevel::ERROR     => 'error',
83
        LogLevel::ALERT     => 'error',
84
        LogLevel::CRITICAL  => 'error',
85
        LogLevel::EMERGENCY => 'error',
86
        LogLevel::WARNING   => 'error',
87
        LogLevel::NOTICE    => 'info'
88
    );
89
90
    /**
91
     * The PID for the running processes.
92
     *
93
     * @var array
94
     */
95
    protected $pid;
96
97
    /**
98
     * The actions unique serial.
99
     *
100
     * @var string
101
     */
102
    protected $serial;
103
104
    /**
105
     * The system logger implementation.
106
     *
107
     * @var \Psr\Log\LoggerInterface
108
     */
109
    protected $systemLogger;
110
111
    /**
112
     * The RegistryProcessor instance to handle running threads.
113
     *
114
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
115
     */
116
    protected $registryProcessor;
117
118
    /**
119
     * The processor to read/write the necessary import data.
120
     *
121
     * @var \TechDivision\Import\Services\ImportProcessorInterface
122
     */
123
    protected $importProcessor;
124
125
    /**
126
     * The system configuration.
127
     *
128
     * @var \TechDivision\Import\ConfigurationInterface
129
     */
130
    protected $configuration;
131
132
    /**
133
     * The input stream to read console information from.
134
     *
135
     * @var \Symfony\Component\Console\Input\InputInterface
136
     */
137
    protected $input;
138
139
    /**
140
     * The output stream to write console information to.
141
     *
142
     * @var \Symfony\Component\Console\Output\OutputInterface
143
     */
144
    protected $output;
145
146
    /**
147
     * The plugins to be processed.
148
     *
149
     * @var array
150
     */
151
    protected $plugins = array();
152
153
    /**
154
     * The constructor to initialize the instance.
155
     *
156
     * @param \Psr\Log\LoggerInterface                                 $systemLogger      The system logger
157
     * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance
158
     * @param \TechDivision\Import\Services\ImportProcessorInterface   $importProcessor   The import processor instance
159
     * @param \TechDivision\Import\ConfigurationInterface              $configuration     The system configuration
160
     * @param \Symfony\Component\Console\Input\InputInterface          $input             An InputInterface instance
161
     * @param \Symfony\Component\Console\Output\OutputInterface        $output            An OutputInterface instance
162
     */
163 1
    public function __construct(
164
        LoggerInterface $systemLogger,
165
        RegistryProcessorInterface $registryProcessor,
166
        ImportProcessorInterface $importProcessor,
167
        ConfigurationInterface $configuration,
168
        InputInterface $input,
169
        OutputInterface $output
170
    ) {
171
172
        // register the shutdown function
173 1
        register_shutdown_function(array($this, 'shutdown'));
174
175
        // initialize the values
176 1
        $this->systemLogger = $systemLogger;
177 1
        $this->registryProcessor = $registryProcessor;
178 1
        $this->importProcessor = $importProcessor;
179 1
        $this->configuration = $configuration;
180 1
        $this->input = $input;
181 1
        $this->output = $output;
182 1
    }
183
184
    /**
185
     * The shutdown handler to catch fatal errors.
186
     *
187
     * This method is need to make sure, that an existing PID file will be removed
188
     * if a fatal error has been triggered.
189
     *
190
     * @return void
191
     */
192
    public function shutdown()
193
    {
194
195
        // check if there was a fatal error caused shutdown
196
        if ($lastError = error_get_last()) {
197
            // initialize error type and message
198
            $type = 0;
199
            $message = '';
200
            // extract the last error values
201
            extract($lastError);
202
            // query whether we've a fatal/user error
203
            if ($type === E_ERROR || $type === E_USER_ERROR) {
204
                // clean-up the PID file
205
                $this->unlock();
206
                // log the fatal error message
207
                $this->log($message, LogLevel::ERROR);
208
            }
209
        }
210
    }
211
212
    /**
213
     * Return's the system logger.
214
     *
215
     * @return \Psr\Log\LoggerInterface The system logger instance
216
     */
217
    public function getSystemLogger()
218
    {
219
        return $this->systemLogger;
220
    }
221
222
    /**
223
     * Return's the RegistryProcessor instance to handle the running threads.
224
     *
225
     * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance
226
     */
227
    public function getRegistryProcessor()
228
    {
229
        return $this->registryProcessor;
230
    }
231
232
    /**
233
     * Return's the import processor instance.
234
     *
235
     * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance
236
     */
237
    public function getImportProcessor()
238
    {
239
        return $this->importProcessor;
240
    }
241
242
    /**
243
     * Return's the system configuration.
244
     *
245
     * @return \TechDivision\Import\ConfigurationInterface The system configuration
246
     */
247
    public function getConfiguration()
248
    {
249
        return $this->configuration;
250
    }
251
252
    /**
253
     * Return's the input stream to read console information from.
254
     *
255
     * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance
256
     */
257
    public function getInput()
258
    {
259
        return $this->input;
260
    }
261
262
    /**
263
     * Return's the output stream to write console information to.
264
     *
265
     * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance
266
     */
267 1
    public function getOutput()
268
    {
269 1
        return $this->output;
270
    }
271
272
    /**
273
     * Return's the unique serial for this import process.
274
     *
275
     * @return string The unique serial
276
     */
277
    public function getSerial()
278
    {
279
        return $this->serial;
280
    }
281
282
    /**
283
     * Persist the UUID of the actual import process to the PID file.
284
     *
285
     * @return void
286
     * @throws \Exception Is thrown, if the PID can not be added
287
     */
288
    public function lock()
289
    {
290
291
        // query whether or not, the PID has already been set
292
        if ($this->pid === $this->getSerial()) {
293
            return;
294
        }
295
296
        // if not, initialize the PID
297
        $this->pid = $this->getSerial();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getSerial() of type string is incompatible with the declared type array of property $pid.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
298
299
        // open the PID file
300
        $fh = fopen($pidFilename = $this->getPidFilename(), 'a');
301
302
        // append the PID to the PID file
303
        if (fwrite($fh, $this->pid . PHP_EOL) === false) {
304
            throw new \Exception(sprintf('Can\'t write PID %s to PID file %s', $this->pid, $pidFilename));
305
        }
306
307
        // close the file handle
308
        fclose($fh);
309
    }
310
311
    /**
312
     * Remove's the UUID of the actual import process from the PID file.
313
     *
314
     * @return void
315
     * @throws \Exception Is thrown, if the PID can not be removed
316
     */
317
    public function unlock()
318
    {
319
        try {
320
            // remove the PID from the PID file if set
321
            if ($this->pid === $this->getSerial()) {
322
                $this->removeLineFromFile($this->pid, $this->getPidFilename());
323
            }
324
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
325
        } catch (FileNotFoundException $fnfe) {
326
            $this->getSystemLogger()->notice(sprintf('PID file %s doesn\'t exist', $this->getPidFilename()));
327
        } catch (LineNotFoundException $lnfe) {
328
            $this->getSystemLogger()->notice(sprintf('PID %s is can not be found in PID file %s', $this->pid, $this->getPidFilename()));
329
        } catch (\Exception $e) {
330
            throw new \Exception(sprintf('Can\'t remove PID %s from PID file %s', $this->pid, $this->getPidFilename()), null, $e);
331
        }
332
    }
333
334
    /**
335
     * Remove's the passed line from the file with the passed name.
336
     *
337
     * @param string $line     The line to be removed
338
     * @param string $filename The name of the file the line has to be removed
339
     *
340
     * @return void
341
     * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed
342
     */
343
    public function removeLineFromFile($line, $filename)
344
    {
345
346
        // query whether or not the filename
347
        if (!file_exists($filename)) {
348
            throw new FileNotFoundException(sprintf('File %s doesn\' exists', $filename));
349
        }
350
351
        // open the PID file
352
        $fh = fopen($filename, 'r+');
353
354
        // initialize the array for the PIDs found in the PID file
355
        $lines = array();
356
357
        // initialize the flag if the line has been found
358
        $found = false;
359
360
        // read the lines with the PIDs from the PID file
361
        while (($buffer = fgets($fh, 4096)) !== false) {
362
            // remove the new line
363
            $buffer = trim($buffer, PHP_EOL);
364
            // if the line is the one to be removed, ignore the line
365
            if ($line === $buffer) {
366
                $found = true;
367
                continue;
368
            }
369
370
            // add the found PID to the array
371
            $lines[] = $buffer;
372
        }
373
374
        // query whether or not, we found the line
375
        if (!$found) {
376
            throw new LineNotFoundException(sprintf('Line %s can not be found in file %s', $line, $filename));
377
        }
378
379
        // if there are NO more lines, delete the file
380
        if (sizeof($lines) === 0) {
381
            fclose($fh);
382
            unlink($filename);
383
            return;
384
        }
385
386
        // empty the file and rewind the file pointer
387
        ftruncate($fh, 0);
388
        rewind($fh);
389
390
        // append the existing lines to the file
391
        foreach ($lines as $ln) {
392
            if (fwrite($fh, $ln . PHP_EOL) === false) {
393
                throw new \Exception(sprintf('Can\'t write %s to file %s', $ln, $filename));
394
            }
395
        }
396
397
        // finally close the file
398
        fclose($fh);
399
    }
400
401
    /**
402
     * Process the given operation.
403
     *
404
     * @return void
405
     * @throws \Exception Is thrown if the operation can't be finished successfully
406
     */
407
    public function process()
408
    {
409
410
        try {
411
            // track the start time
412
            $startTime = microtime(true);
413
414
            // prepare the global data for the import process
415
            $this->setUp();
416
417
            // process the plugins defined in the configuration
418
            foreach ($this->getConfiguration()->getPlugins() as $pluginConfiguration) {
419
                $this->pluginFactory($pluginConfiguration)->process();
420
            }
421
422
            // tear down the  instance
423
            $this->tearDown();
424
425
            // track the time needed for the import in seconds
426
            $endTime = microtime(true) - $startTime;
427
428
            // log a message that import has been finished
429
            $this->log(
430
                sprintf(
431
                    'Successfully finished import with serial %s in %f s',
432
                    $this->getSerial(),
433
                    $endTime
434
                ),
435
                LogLevel::INFO
436
            );
437
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
438
        } catch (\Exception $e) {
439
            // tear down
440
            $this->tearDown();
441
442
            // track the time needed for the import in seconds
443
            $endTime = microtime(true) - $startTime;
444
445
            // log a message that the file import failed
446
            $this->getSystemLogger()->error($e->__toString());
447
448
            // log a message that import has been finished
449
            $this->getSystemLogger()->info(
450
                sprintf(
451
                    'Can\'t finish import with serial %s in %f s',
452
                    $this->getSerial(),
453
                    $endTime
454
                )
455
            );
456
457
            // re-throw the exception
458
            throw $e;
459
        }
460
    }
461
462
    /**
463
     * Factory method to create new plugin instances.
464
     *
465
     * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance
466
     *
467
     * @return object The plugin instance
468
     */
469
    protected function pluginFactory(PluginConfigurationInterface $pluginConfiguration)
470
    {
471
472
        // load the plugin class name
473
        $className = $pluginConfiguration->getClassName();
474
475
        // initialize and return the plugin instance
476
        return new $className($this, $pluginConfiguration);
477
    }
478
479
    /**
480
     * Lifecycle callback that will be inovked before the
481
     * import process has been started.
482
     *
483
     * @return void
484
     */
485
    protected function setUp()
486
    {
487
488
        // generate the serial for the new job
489
        $this->serial = Uuid::uuid4()->__toString();
490
491
        // query whether or not an import is running AND an existing PID has to be ignored
492
        if (file_exists($pidFilename = $this->getPidFilename()) && !$this->getConfiguration()->isIgnorePid()) {
493
            throw new \Exception(sprintf('At least one import process is already running (check PID: %s)', $pidFilename));
494
        } elseif (file_exists($pidFilename = $this->getPidFilename()) && $this->getConfiguration()->isIgnorePid()) {
495
            $this->log(sprintf('At least one import process is already running (PID: %s)', $pidFilename), LogLevel::WARNING);
496
        }
497
498
        // write the TechDivision ANSI art icon to the console
499
        $this->log($this->ansiArt);
500
501
        // log the debug information, if debug mode is enabled
502
        if ($this->getConfiguration()->isDebugMode()) {
503
            // log the system's PHP configuration
504
            $this->log(sprintf('PHP version: %s', phpversion()), LogLevel::DEBUG);
505
            $this->log('-------------------- Loaded Extensions -----------------------', LogLevel::DEBUG);
506
            $this->log(implode(', ', $loadedExtensions = get_loaded_extensions()), LogLevel::DEBUG);
507
            $this->log('--------------------------------------------------------------', LogLevel::DEBUG);
508
509
            // write a warning for low performance, if XDebug extension is activated
510
            if (in_array('xdebug', $loadedExtensions)) {
511
                $this->log('Low performance exptected, as result of enabled XDebug extension!', LogLevel::WARNING);
512
            }
513
        }
514
515
        // log a message that import has been started
516
        $this->log(
517
            sprintf(
518
                'Now start import with serial %s (operation: %s)',
519
                $this->getSerial(),
520
                $this->getConfiguration()->getOperationName()
521
            ),
522
            LogLevel::INFO
523
        );
524
525
        // initialize the status
526
        $status = array(
527
            RegistryKeys::STATUS => 1,
528
            RegistryKeys::BUNCHES => 0,
529
            RegistryKeys::SOURCE_DIRECTORY => $this->getConfiguration()->getSourceDir()
530
        );
531
532
        // append it to the registry
533
        $this->getRegistryProcessor()->setAttribute($this->getSerial(), $status);
534
    }
535
536
    /**
537
     * Lifecycle callback that will be inovked after the
538
     * import process has been finished.
539
     *
540
     * @return void
541
     */
542
    protected function tearDown()
543
    {
544
        $this->getRegistryProcessor()->removeAttribute($this->getSerial());
545
    }
546
547
    /**
548
     * Simple method that writes the passed method the the console and the
549
     * system logger, if configured and a log level has been passed.
550
     *
551
     * @param string $msg      The message to log
552
     * @param string $logLevel The log level to use
553
     *
554
     * @return void
555
     */
556
    protected function log($msg, $logLevel = null)
557
    {
558
559
        // initialize the formatter helper
560
        $helper = new FormatterHelper();
561
562
        // map the log level to the console style
563
        $style = $this->mapLogLevelToStyle($logLevel);
564
565
        // format the message, according to the passed log level and write it to the console
566
        $this->getOutput()->writeln($logLevel ? $helper->formatBlock($msg, $style) : $msg);
567
568
        // log the message if a log level has been passed
569
        if ($logLevel && $systemLogger = $this->getSystemLogger()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $logLevel of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
570
            $systemLogger->log($logLevel, $msg);
571
        }
572
    }
573
574
    /**
575
     * Map's the passed log level to a valid symfony console style.
576
     *
577
     * @param string $logLevel The log level to map
578
     *
579
     * @return string The apropriate symfony console style
580
     */
581
    protected function mapLogLevelToStyle($logLevel)
582
    {
583
584
        // query whether or not the log level is mapped
585
        if (isset($this->logLevelStyleMapping[$logLevel])) {
586
            return $this->logLevelStyleMapping[$logLevel];
587
        }
588
589
        // return the default style => info
590
        return Simple::DEFAULT_STYLE;
591
    }
592
593
    /**
594
     * Return's the PID filename to use.
595
     *
596
     * @return string The PID filename
597
     */
598
    protected function getPidFilename()
599
    {
600
        return $this->getConfiguration()->getPidFilename();
601
    }
602
}
603