ImportCreateConfigurationFileCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 29
c 3
b 0
f 0
dl 0
loc 86
ccs 0
cts 27
cp 0
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
B executeSimpleCommand() 0 55 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Command\ImportCreateConfigurationFileCommand
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2016 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-cli-simple
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Cli\Command;
16
17
use JMS\Serializer\SerializerBuilder;
18
use JMS\Serializer\XmlSerializationVisitor;
19
use JMS\Serializer\JsonSerializationVisitor;
20
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
21
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use TechDivision\Import\Utils\CommandNames;
26
use TechDivision\Import\Configuration\ConfigurationInterface;
27
use TechDivision\Import\Utils\InputOptionKeysInterface;
28
29
/**
30
 * The command implementation that creates a configuration file from one of the templates.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2016 TechDivision GmbH <[email protected]>
34
 * @license   https://opensource.org/licenses/MIT
35
 * @link      https://github.com/techdivision/import-cli-simple
36
 * @link      http://www.techdivision.com
37
 */
38
class ImportCreateConfigurationFileCommand extends AbstractSimpleImportCommand
39
{
40
41
    /**
42
     * Configures the current command.
43
     *
44
     * @return void
45
     * @see \Symfony\Component\Console\Command\Command::configure()
46
     */
47
    protected function configure()
48
    {
49
50
        // initialize the command with the required/optional options
51
        $this->setName(CommandNames::IMPORT_CREATE_CONFIGURATION_FILE)
52
             ->setDescription('Create\'s a configuration file from the given entity\'s template')
53
             ->addOption(InputOptionKeysInterface::DEST, null, InputOption::VALUE_REQUIRED, 'The relative/absolut pathname of the destination file');
54
55
        // invoke the parent method
56
        parent::configure();
57
    }
58
59
60
    /**
61
     * Finally executes the simple command.
62
     *
63
     * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance
64
     * @param \Symfony\Component\Console\Input\InputInterface           $input         An InputInterface instance
65
     * @param \Symfony\Component\Console\Output\OutputInterface         $output        An OutputInterface instance
66
     *
67
     * @return void
68
     */
69
    protected function executeSimpleCommand(
70
        ConfigurationInterface $configuration,
71
        InputInterface $input,
72
        OutputInterface $output
73
    ) {
74
75
        // initialie the directory where we want to export the configuration to
76
        $exportDir = $input->hasParameterOption(InputOptionKeysInterface::CUSTOM_CONFIGURATION_DIR) ? $input->getOption(InputOptionKeysInterface::CUSTOM_CONFIGURATION_DIR) : $configuration->getInstallationDir();
77
78
        // initialize the configuration filename
79
        $configurationFilename = $input->getOption(InputOptionKeysInterface::DEST);
80
        if ($configurationFilename === null) {
81
            $configurationFilename = sprintf('%s/app/etc/techdivision-import.json', $exportDir);
82
        }
83
84
        // extract the format from the configuration file suffix
85
        $format = pathinfo($configurationFilename, PATHINFO_EXTENSION);
86
87
        // initialize the serializer
88
        $builder = SerializerBuilder::create();
89
        $builder->addDefaultSerializationVisitors();
90
91
        // initialize the naming strategy
92
        $namingStrategy = new SerializedNameAnnotationStrategy(new IdenticalPropertyNamingStrategy());
93
94
        // create the configuration based on the given configuration file suffix
95
        switch ($format) {
96
            // initialize the JSON visitor
97
            case 'json':
98
                // initialize the visitor because we want to set JSON options
99
                $visitor = new JsonSerializationVisitor($namingStrategy);
0 ignored issues
show
Bug introduced by
$namingStrategy of type JMS\Serializer\Naming\Se...dNameAnnotationStrategy is incompatible with the type integer expected by parameter $options of JMS\Serializer\JsonSeria...nVisitor::__construct(). ( Ignorable by Annotation )

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

99
                $visitor = new JsonSerializationVisitor(/** @scrutinizer ignore-type */ $namingStrategy);
Loading history...
100
                $visitor->setOptions(JSON_PRETTY_PRINT);
0 ignored issues
show
Bug introduced by
The method setOptions() does not exist on JMS\Serializer\JsonSerializationVisitor. ( Ignorable by Annotation )

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

100
                $visitor->/** @scrutinizer ignore-call */ 
101
                          setOptions(JSON_PRETTY_PRINT);

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...
101
                break;
102
103
            // initialize the XML visitor
104
            case 'xml':
105
                $visitor = new XmlSerializationVisitor($namingStrategy);
0 ignored issues
show
Bug introduced by
$namingStrategy of type JMS\Serializer\Naming\Se...dNameAnnotationStrategy is incompatible with the type boolean expected by parameter $formatOutput of JMS\Serializer\XmlSerial...nVisitor::__construct(). ( Ignorable by Annotation )

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

105
                $visitor = new XmlSerializationVisitor(/** @scrutinizer ignore-type */ $namingStrategy);
Loading history...
106
                break;
107
108
            // throw an execption in all other cases
109
            default:
110
                throw new \Exception(sprintf('Found invalid configuration format "%s"', $format));
0 ignored issues
show
Bug introduced by
It seems like $format can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|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

110
                throw new \Exception(sprintf('Found invalid configuration format "%s"', /** @scrutinizer ignore-type */ $format));
Loading history...
111
        }
112
113
        // register the visitor in the builder instance
114
        $builder->setSerializationVisitor($format, $visitor);
0 ignored issues
show
Bug introduced by
$visitor of type JMS\Serializer\JsonSeria...XmlSerializationVisitor is incompatible with the type JMS\Serializer\Visitor\F...alizationVisitorFactory expected by parameter $visitor of JMS\Serializer\Serialize...tSerializationVisitor(). ( Ignorable by Annotation )

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

114
        $builder->setSerializationVisitor($format, /** @scrutinizer ignore-type */ $visitor);
Loading history...
115
116
        // finally create the serializer instance
117
        $serializer = $builder->build();
118
119
        // try to write the configuration file to the actual working directory
120
        if (file_put_contents($configurationFilename, $serializer->serialize($configuration, $format))) {
121
            $output->writeln(sprintf('<info>Successfully written configuration file %s</info>', $configurationFilename));
122
        } else {
123
            $output->writeln(sprintf('<error>Can\'t write configuration file %s</error>', $configurationFilename));
124
        }
125
    }
126
}
127