Completed
Push — master ( 1235b0...90fadd )
by Tim
15s
created

AbstractSimpleImportCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Command\AbstractSimpleImportCommand
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 Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use TechDivision\Import\ConfigurationInterface;
28
use TechDivision\Import\Configuration\Jms\Configuration;
29
use TechDivision\Import\Cli\Utils\DependencyInjectionKeys;
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   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
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
    protected function configure()
50
    {
51
52
        // configure the command
53
        $this->addOption(InputOptionKeys::PID_FILENAME, null, InputOption::VALUE_REQUIRED, 'The explicit PID filename to use', sprintf('%s/%s', sys_get_temp_dir(), Configuration::PID_FILENAME))
54
             ->addOption(InputOptionKeys::INSTALLATION_DIR, null, InputOption::VALUE_REQUIRED, 'The Magento installation directory to which the files has to be imported', $installationDir = getcwd())
55
             ->addOption(InputOptionKeys::SOURCE_DIR, null, InputOption::VALUE_REQUIRED, 'The directory that has to be watched for new files', sprintf('%s/var/importexport', $installationDir))
56
             ->addOption(InputOptionKeys::MAGENTO_EDITION, null, InputOption::VALUE_REQUIRED, 'The Magento edition to be used, either one of "CE" or "EE"', 'CE')
57
             ->addOption(InputOptionKeys::CONFIGURATION, null, InputOption::VALUE_REQUIRED, 'Specify the pathname to the configuration file to use')
58
             ->addOption(InputOptionKeys::LOG_LEVEL, null, InputOption::VALUE_REQUIRED, 'The log level to use');
59
    }
60
61
    /**
62
     * Return's the container instance.
63
     *
64
     * @return \Symfony\Component\DependencyInjection\ContainerInterface The container instance
65
     */
66
    protected function getContainer()
67
    {
68
        return $this->getApplication()->getContainer();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getContainer() does only exist in the following sub-classes of Symfony\Component\Console\Application: TechDivision\Import\Cli\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
69
    }
70
71
    /**
72
     * Executes the current command.
73
     *
74
     * This method is not abstract because you can use this class
75
     * as a concrete class. In this case, instead of defining the
76
     * execute() method, you set the code to execute by passing
77
     * a Closure to the setCode() method.
78
     *
79
     * @param \Symfony\Component\Console\Input\InputInterface   $input  An InputInterface instance
80
     * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance
81
     *
82
     * @return null|int null or 0 if everything went fine, or an error code
83
     * @throws \LogicException When this abstract method is not implemented
84
     * @see \Symfony\Component\Console\Command\Command::execute()
85
     */
86
    protected function execute(InputInterface $input, OutputInterface $output)
87
    {
88
89
        // try to load the configuration file
90
        $configuration = $this->getContainer()->get(DependencyInjectionKeys::CONFIGURATION_SIMPLE);
91
92
        // finally execute the simple command
93
        $this->executeSimpleCommand($configuration, $input, $output);
94
    }
95
96
    /**
97
     * Finally executes the simple command.
98
     *
99
     * @param \TechDivision\Import\ConfigurationInterface       $configuration The configuration instance
100
     * @param \Symfony\Component\Console\Input\InputInterface   $input         An InputInterface instance
101
     * @param \Symfony\Component\Console\Output\OutputInterface $output        An OutputInterface instance
102
     *
103
     * @return void
104
     */
105
    abstract protected function executeSimpleCommand(
106
        ConfigurationInterface $configuration,
107
        InputInterface $input,
108
        OutputInterface $output
109
    );
110
}
111