Passed
Push — 8.x ( ab4097...e0db1c )
by Tim
09:12
created

ImportCreateConfigurationFileCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 89
ccs 0
cts 23
cp 0
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
B executeSimpleCommand() 0 58 7
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 JMS\Serializer\XmlSerializationVisitor;
25
use JMS\Serializer\YamlSerializationVisitor;
26
use JMS\Serializer\JsonSerializationVisitor;
27
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
28
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Output\OutputInterface;
31
use Symfony\Component\Console\Input\InputOption;
32
use TechDivision\Import\Utils\CommandNames;
33
use TechDivision\Import\ConfigurationInterface;
34
35
/**
36
 * The command implementation that creates a configuration file from one of the templates.
37
 *
38
 * @author    Tim Wagner <[email protected]>
39
 * @copyright 2016 TechDivision GmbH <[email protected]>
40
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
41
 * @link      https://github.com/techdivision/import-cli-simple
42
 * @link      http://www.techdivision.com
43
 */
44
class ImportCreateConfigurationFileCommand extends AbstractSimpleImportCommand
45
{
46
47
    /**
48
     * Configures the current command.
49
     *
50
     * @return void
51
     * @see \Symfony\Component\Console\Command\Command::configure()
52
     */
53
    protected function configure()
54
    {
55
56
        // initialize the command with the required/optional options
57
        $this->setName(CommandNames::IMPORT_CREATE_CONFIGURATION_FILE)
58
             ->setDescription('Create\'s a configuration file from the given entity\'s template')
59
             ->addOption(InputOptionKeys::DEST, null, InputOption::VALUE_REQUIRED, 'The relative/absolut pathname of the destination file');
60
61
        // invoke the parent method
62
        parent::configure();
63
    }
64
65
66
    /**
67
     * Finally executes the simple command.
68
     *
69
     * @param \TechDivision\Import\ConfigurationInterface       $configuration The configuration instance
70
     * @param \Symfony\Component\Console\Input\InputInterface   $input         An InputInterface instance
71
     * @param \Symfony\Component\Console\Output\OutputInterface $output        An OutputInterface instance
72
     *
73
     * @return void
74
     */
75
    protected function executeSimpleCommand(
76
        ConfigurationInterface $configuration,
77
        InputInterface $input,
78
        OutputInterface $output
79
    ) {
80
81
        // initialize the configuration filename
82
        $configurationFilename = $input->getOption(InputOptionKeys::DEST);
83
        if ($configurationFilename === null) {
84
            $configurationFilename = sprintf('%s/app/etc/techdivision-import.json', $configuration->getCustom());
0 ignored issues
show
Bug introduced by
The method getCustom() does not exist on TechDivision\Import\ConfigurationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
            $configurationFilename = sprintf('%s/app/etc/techdivision-import.json', $configuration->/** @scrutinizer ignore-call */ getCustom());

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.

Loading history...
85
        }
86
87
        // extract the format from the configuration file suffix
88
        $format = pathinfo($configurationFilename, PATHINFO_EXTENSION);
0 ignored issues
show
Bug introduced by
It seems like $configurationFilename can also be of type string[]; however, parameter $path of pathinfo() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        $format = pathinfo(/** @scrutinizer ignore-type */ $configurationFilename, PATHINFO_EXTENSION);
Loading history...
89
90
        // initialize the serializer
91
        $builder = SerializerBuilder::create();
92
        $builder->addDefaultSerializationVisitors();
93
94
        // initialize the naming strategy
95
        $namingStrategy = new SerializedNameAnnotationStrategy(new IdenticalPropertyNamingStrategy());
96
97
        // create the configuration based on the given configuration file suffix
98
        switch ($format) {
99
            // initialize the JSON visitor
100
            case 'json':
101
                // initialize the visitor because we want to set JSON options
102
                $visitor = new JsonSerializationVisitor($namingStrategy);
103
                $visitor->setOptions(JSON_PRETTY_PRINT);
104
                break;
105
106
            // initialize the XML visitor
107
            case 'xml':
108
                $visitor = new XmlSerializationVisitor($namingStrategy);
109
                break;
110
111
            // initialize the YAML visitor
112
            case 'yml':
113
            case 'yaml':
114
                $visitor = new YamlSerializationVisitor($namingStrategy);
115
                break;
116
117
            // throw an execption in all other cases
118
            default:
119
               throw new \Exception(sprintf('Found invalid configuration format "%s"', $format));
120
        }
121
122
        // register the visitor in the builder instance
123
        $builder->setSerializationVisitor($format, $visitor);
124
125
        // finally create the serializer instance
126
        $serializer = $builder->build();
127
128
        // try to write the configuration file to the actual working directory
129
        if (file_put_contents($configurationFilename, $serializer->serialize($configuration, $format))) {
0 ignored issues
show
Bug introduced by
It seems like $configurationFilename can also be of type string[]; however, parameter $filename of file_put_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
        if (file_put_contents(/** @scrutinizer ignore-type */ $configurationFilename, $serializer->serialize($configuration, $format))) {
Loading history...
130
            $output->writeln(sprintf('<info>Successfully written configuration file %s</info>', $configurationFilename));
0 ignored issues
show
Bug introduced by
It seems like $configurationFilename can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
            $output->writeln(sprintf('<info>Successfully written configuration file %s</info>', /** @scrutinizer ignore-type */ $configurationFilename));
Loading history...
131
        } else {
132
            $output->writeln(sprintf('<error>Can\'t write configuration file %s</error>', $configurationFilename));
133
        }
134
    }
135
}
136