Completed
Push — master ( 990fd5...f17d9c )
by Tim
11s
created

ImportCreateConfigurationFileCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

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 11
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Command\ImportCreateConfigurationFileCommand
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 JMS\Serializer\SerializerBuilder;
24
use TechDivision\Import\Utils\CommandNames;
25
use TechDivision\Import\ConfigurationInterface;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
30
/**
31
 * The command implementation that creates a configuration file from one of the templates.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-cli-simple
37
 * @link      http://www.techdivision.com
38
 */
39
class ImportCreateConfigurationFileCommand extends AbstractSimpleImportCommand
40
{
41
42
    /**
43
     * Configures the current command.
44
     *
45
     * @return void
46
     * @see \Symfony\Component\Console\Command\Command::configure()
47
     */
48
    protected function configure()
49
    {
50
51
        // initialize the command with the required/optional options
52
        $this->setName(CommandNames::IMPORT_CREATE_CONFIGURATION_FILE)
53
             ->setDescription('Create\'s a configuration file from the given entity\'s template')
54
             ->addOption(InputOptionKeys::DEST, null, InputOption::VALUE_REQUIRED, 'The relative/absolut pathname of the destination file');
55
56
        // invoke the parent method
57
        parent::configure();
58
    }
59
60
61
    /**
62
     * Finally executes the simple command.
63
     *
64
     * @param \TechDivision\Import\ConfigurationInterface       $configuration The configuration instance
65
     * @param \Symfony\Component\Console\Input\InputInterface   $input         An InputInterface instance
66
     * @param \Symfony\Component\Console\Output\OutputInterface $output        An OutputInterface instance
67
     *
68
     * @return void
69
     */
70
    protected function executeSimpleCommand(
71
        ConfigurationInterface $configuration,
72
        InputInterface $input,
73
        OutputInterface $output
74
    ) {
75
76
        // initialize the configuration filename
77
        $configurationFilename = $input->getOption(InputOptionKeys::DEST);
78
        if ($configurationFilename === null) {
79
            $configurationFilename = sprintf('%s/techdivision-import.json', $configuration->getInstallationDir());
80
        }
81
82
        // initialize the serializer
83
        $serializer = SerializerBuilder::create()->build();
84
85
        // try to write the configuration file to the actual working directory
86
        if (file_put_contents($configurationFilename, $serializer->serialize($configuration, pathinfo($configurationFilename, PATHINFO_EXTENSION)))) {
87
            $output->writeln(sprintf('<info>Successfully written configuration file %s</info>', $configurationFilename));
88
        } else {
89
            $output->writeln(sprintf('<error>Can\'t write configuration file %s</error>', $configurationFilename));
90
        }
91
    }
92
}
93