Passed
Push — master ( 4dcb1e...b0665a )
by
unknown
23:07 queued 16:49
created

AbstractSimpleImportCommand::createSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 0
cts 9
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Command\AbstractSimpleImportCommand
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\Naming\IdenticalPropertyNamingStrategy;
18
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
19
use JMS\Serializer\Serializer;
20
use JMS\Serializer\SerializerBuilder;
21
use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use TechDivision\Import\Configuration\ConfigurationInterface;
27
use TechDivision\Import\Configuration\Jms\Configuration;
28
use TechDivision\Import\Cli\Utils\DependencyInjectionKeys;
29
use TechDivision\Import\Utils\InputOptionKeysInterface;
30
31
/**
32
 * Abstract command implementation for simple import commands (not using Importer class).
33
 *
34
 * @author    Tim Wagner <[email protected]>
35
 * @copyright 2016 TechDivision GmbH <[email protected]>
36
 * @license   https://opensource.org/licenses/MIT
37
 * @link      https://github.com/techdivision/import-cli-simple
38
 * @link      http://www.techdivision.com
39
 */
40
abstract class AbstractSimpleImportCommand extends Command
41
{
42
43
    /**
44
     * Configures the current command.
45
     *
46
     * @return void
47
     * @see \Symfony\Component\Console\Command\Command::configure()
48
     */
49 4
    protected function configure()
50
    {
51
52
        // configure the command
53 4
        $this->addOption(InputOptionKeysInterface::PID_FILENAME, null, InputOption::VALUE_REQUIRED, 'The explicit PID filename to use', sprintf('%s/%s', sys_get_temp_dir(), Configuration::PID_FILENAME))
54 4
             ->addOption(InputOptionKeysInterface::INSTALLATION_DIR, null, InputOption::VALUE_REQUIRED, 'The Magento installation directory to which the files has to be imported', getcwd())
55 4
             ->addOption(InputOptionKeysInterface::CONFIGURATION_DIR, null, InputOption::VALUE_OPTIONAL, 'The Magento configuration directory')
56 4
             ->addOption(InputOptionKeysInterface::SYSTEM_NAME, null, InputOption::VALUE_REQUIRED, 'Specify the system name to use', gethostname())
57 4
             ->addOption(InputOptionKeysInterface::SOURCE_DIR, null, InputOption::VALUE_REQUIRED, 'The directory that has to be watched for new files')
58 4
             ->addOption(InputOptionKeysInterface::MAGENTO_VERSION, null, InputOption::VALUE_REQUIRED, 'The Magento version to be used, e. g. "2.1.2"')
59 4
             ->addOption(InputOptionKeysInterface::MAGENTO_EDITION, null, InputOption::VALUE_REQUIRED, 'The Magento edition to be used, either one of "CE" or "EE"', 'CE')
60 4
             ->addOption(InputOptionKeysInterface::CONFIGURATION, null, InputOption::VALUE_REQUIRED, 'Specify the pathname to the configuration file to use')
61 4
             ->addOption(InputOptionKeysInterface::CUSTOM_CONFIGURATION_DIR, null, InputOption::VALUE_REQUIRED, 'The path to the custom configuration directory')
62 4
             ->addOption(InputOptionKeysInterface::CUSTOM_CONFIGURATION_PUBLIC_DIR, null, InputOption::VALUE_OPTIONAL, 'The path to the custom configuration public directory')
63 4
             ->addOption(InputOptionKeysInterface::LOG_LEVEL, null, InputOption::VALUE_REQUIRED, 'The log level to use')
64 4
             ->addOption(InputOptionKeysInterface::LOG_FILE, null, InputOption::VALUE_REQUIRED, 'The log file to use');
65
    }
66
67
    /**
68
     * Return's the container instance.
69
     *
70
     * @return \Symfony\Component\DependencyInjection\ContainerInterface The container instance
71
     */
72
    protected function getContainer()
73
    {
74
        return $this->getApplication()->getContainer();
0 ignored issues
show
Bug introduced by
The method getContainer() does not exist on Symfony\Component\Console\Application. It seems like you code against a sub-type of Symfony\Component\Console\Application such as TechDivision\Import\Cli\Application. ( Ignorable by Annotation )

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

74
        return $this->getApplication()->/** @scrutinizer ignore-call */ getContainer();
Loading history...
75
    }
76
77
    /**
78
     * Executes the current command.
79
     *
80
     * This method is not abstract because you can use this class
81
     * as a concrete class. In this case, instead of defining the
82
     * execute() method, you set the code to execute by passing
83
     * a Closure to the setCode() method.
84
     *
85
     * @param \Symfony\Component\Console\Input\InputInterface   $input  An InputInterface instance
86
     * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance
87
     *
88
     * @return null|int null or 0 if everything went fine, or an error code
89
     * @throws \LogicException When this abstract method is not implemented
90
     * @see \Symfony\Component\Console\Command\Command::execute()
91
     */
92
    protected function execute(InputInterface $input, OutputInterface $output)
93
    {
94
95
        // try to load the configuration file
96
        $configuration = $this->getContainer()->get(DependencyInjectionKeys::CONFIGURATION_SIMPLE);
97
98
        // finally execute the simple command
99
        return $this->executeSimpleCommand($configuration, $input, $output);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->executeSimpleComm...ation, $input, $output) targeting TechDivision\Import\Cli\...:executeSimpleCommand() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
It seems like $configuration can also be of type null; however, parameter $configuration of TechDivision\Import\Cli\...:executeSimpleCommand() does only seem to accept TechDivision\Import\Conf...\ConfigurationInterface, 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

99
        return $this->executeSimpleCommand(/** @scrutinizer ignore-type */ $configuration, $input, $output);
Loading history...
Bug Best Practice introduced by
The expression return $this->executeSim...ation, $input, $output) returns the type void which is incompatible with the documented return type integer|null.
Loading history...
100
    }
101
102
    /**
103
     * create json serializer for configs
104
     *
105
     * @return Serializer
106
     */
107
    protected function createSerializer(): Serializer
108
    {
109
        $format = 'json';
110
        $builder = SerializerBuilder::create();
111
        $builder->addDefaultSerializationVisitors();
112
        $namingStrategy = new SerializedNameAnnotationStrategy(new IdenticalPropertyNamingStrategy());
113
114
        // register the visitor in the builder instance
115
        $visitor = new JsonSerializationVisitorFactory($namingStrategy);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\Visitor\F...rFactory::__construct() has too many arguments starting with $namingStrategy. ( Ignorable by Annotation )

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

115
        $visitor = /** @scrutinizer ignore-call */ new JsonSerializationVisitorFactory($namingStrategy);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
116
        $visitor->setOptions(JSON_PRETTY_PRINT);
117
        $builder->setSerializationVisitor($format, $visitor);
118
        return $builder->build();
119
    }
120
121
    /**
122
     * Finally executes the simple command.
123
     *
124
     * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance
125
     * @param \Symfony\Component\Console\Input\InputInterface           $input         An InputInterface instance
126
     * @param \Symfony\Component\Console\Output\OutputInterface         $output        An OutputInterface instance
127
     *
128
     * @return void
129
     */
130
    abstract protected function executeSimpleCommand(
131
        ConfigurationInterface $configuration,
132
        InputInterface $input,
133
        OutputInterface $output
134
    );
135
}
136