Completed
Push — master ( 70cce3...6b7465 )
by Tim
02:52
created

ImportClearPidCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 0
cts 18
cp 0
rs 9.4285
cc 1
eloc 14
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Command\ImportClearPidCommand
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\Command;
22
23
use Psr\Log\LogLevel;
24
use Monolog\Logger;
25
use Monolog\Handler\ErrorLogHandler;
26
use JMS\Serializer\SerializerBuilder;
27
use TechDivision\Import\Cli\Simple;
28
use TechDivision\Import\Cli\Configuration;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Input\InputOption;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Output\OutputInterface;
33
34
/**
35
 * The remove PID command implementation.
36
 *
37
 * @author    Tim Wagner <[email protected]>
38
 * @copyright 2016 TechDivision GmbH <[email protected]>
39
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
40
 * @link      https://github.com/techdivision/import-cli-simple
41
 * @link      http://www.techdivision.com
42
 */
43
class ImportClearPidCommand extends Command
44
{
45
46
    /**
47
     * Configures the current command.
48
     *
49
     * @return void
50
     * @see \Symfony\Component\Console\Command\Command::configure()
51
     */
52
    protected function configure()
53
    {
54
55
        // initialize the command with the required/optional options
56
        $this->setName('import:clear:pid')
57
             ->setDescription('Clears the PID file from a previous import process, if it has not been cleaned up')
58
             ->addOption(
59
                 InputOptionKeys::CONFIGURATION,
60
                 null,
61
                 InputOption::VALUE_REQUIRED,
62
                 'Specify the pathname to the configuration file to use',
63
                 sprintf('%s/techdivision-import.json', getcwd())
64
             )
65
             ->addOption(
66
                 InputOptionKeys::LOG_LEVEL,
67
                 null,
68
                 InputOption::VALUE_REQUIRED,
69
                 'The log level to use'
70
             );
71
    }
72
73
    /**
74
     * Factory implementation to create a new initialized configuration instance.
75
     *
76
     * If command line options are specified, they will always override the
77
     * values found in the configuration file.
78
     *
79
     * @param \Symfony\Component\Console\Input\InputInterface $input The Symfony console input instance
80
     *
81
     * @return \TechDivision\Import\Cli\Configuration The configuration instance
82
     * @throws \Exception Is thrown, if the specified configuration file doesn't exist
83
     */
84
    protected function configurationFactory(InputInterface $input)
85
    {
86
87
        // load the configuration filename we want to use
88
        $filename = $input->getOption(InputOptionKeys::CONFIGURATION);
89
90
        // load the JSON data
91
        if (!$jsonData = file_get_contents($filename)) {
92
            throw new \Exception(sprintf('Can\'t load configuration file %s', $filename));
93
        }
94
95
        // initialize the JMS serializer and load the configuration
96
        $serializer = SerializerBuilder::create()->build();
97
        /** @var \TechDivision\Import\Cli\Configuration $instance */
98
        $instance = $serializer->deserialize($jsonData, 'TechDivision\Import\Cli\Configuration', 'json');
99
100
        // query whether or not the log level has been specified as command line
101
        // option, if yes override the value from the configuration file
102
        if ($logLevel = $input->getOption(InputOptionKeys::LOG_LEVEL)) {
103
            $instance->setLogLevel($logLevel);
104
        }
105
106
        // return the initialized configuration instance
107
        return $instance;
108
    }
109
110
    /**
111
     * Executes the current command.
112
     *
113
     * This method is not abstract because you can use this class
114
     * as a concrete class. In this case, instead of defining the
115
     * execute() method, you set the code to execute by passing
116
     * a Closure to the setCode() method.
117
     *
118
     * @param \Symfony\Component\Console\Input\InputInterface   $input  An InputInterface instance
119
     * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance
120
     *
121
     * @return null|int null or 0 if everything went fine, or an error code
122
     * @throws \LogicException When this abstract method is not implemented
123
     * @see \Symfony\Component\Console\Command\Command::execute()
124
     */
125
    protected function execute(InputInterface $input, OutputInterface $output)
126
    {
127
128
        // register the JMS Serializer annotations
129
        \Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace(
130
            'JMS\Serializer\Annotation',
131
            dirname(__DIR__).'/../vendor/jms/serializer/src'
132
        );
133
134
        // load the specified configuration
135
        $configuration = $this->configurationFactory($input);
136
137
        // initialize the system logger
138
        $systemLogger = new Logger('techdivision/import');
139
        $systemLogger->pushHandler(
140
            new ErrorLogHandler(
141
                ErrorLogHandler::OPERATING_SYSTEM,
142
                $configuration->getLogLevel()
143
            )
144
        );
145
146
        // initialize and run the importer
147
        $importer = new Simple();
148
        $importer->setInput($input);
149
        $importer->setOutput($output);
150
        $importer->setSystemLogger($systemLogger);
151
        $importer->setConfiguration($configuration);
152
        $importer->clearPid();
153
    }
154
}
155