1 | <?php |
||
41 | abstract class AbstractImportCommand extends Command |
||
42 | { |
||
43 | |||
44 | /** |
||
45 | * Configures the current command. |
||
46 | * |
||
47 | * @return void |
||
48 | * @see \Symfony\Component\Console\Command\Command::configure() |
||
49 | */ |
||
50 | protected function configure() |
||
51 | { |
||
52 | |||
53 | // configure the command |
||
54 | $this->addArgument(InputArgumentKeys::OPERATION_NAME, InputArgument::OPTIONAL, 'The operation that has to be used for the import, one of "add-update", "replace" or "delete"', OperationKeys::ADD_UPDATE) |
||
55 | ->addOption(InputOptionKeys::INSTALLATION_DIR, null, InputOption::VALUE_REQUIRED, 'The Magento installation directory to which the files has to be imported', getcwd()) |
||
56 | ->addOption(InputOptionKeys::SYSTEM_NAME, null, InputOption::VALUE_REQUIRED, 'Specify the system name to use', gethostname()) |
||
57 | ->addOption(InputOptionKeys::PID_FILENAME, null, InputOption::VALUE_REQUIRED, 'The explicit PID filename to use', sprintf('%s/%s', sys_get_temp_dir(), Configuration::PID_FILENAME)) |
||
58 | ->addOption(InputOptionKeys::MAGENTO_EDITION, null, InputOption::VALUE_REQUIRED, 'The Magento edition to be used, either one of "CE" or "EE"', 'CE') |
||
59 | ->addOption(InputOptionKeys::MAGENTO_VERSION, null, InputOption::VALUE_REQUIRED, 'The Magento version to be used, e. g. "2.1.2"') |
||
60 | ->addOption(InputOptionKeys::CONFIGURATION, null, InputOption::VALUE_REQUIRED, 'Specify the pathname to the configuration file to use') |
||
61 | ->addOption(InputOptionKeys::ENTITY_TYPE_CODE, null, InputOption::VALUE_REQUIRED, 'Specify the entity type code to use, either one of "catalog_product", "catalog_category" or "eav_attribute"') |
||
62 | ->addOption(InputOptionKeys::SOURCE_DIR, null, InputOption::VALUE_REQUIRED, 'The directory that has to be watched for new files') |
||
63 | ->addOption(InputOptionKeys::TARGET_DIR, null, InputOption::VALUE_REQUIRED, 'The target directory with the files that has been imported') |
||
64 | ->addOption(InputOptionKeys::ARCHIVE_DIR, null, InputOption::VALUE_REQUIRED, 'The directory the imported files will be archived in') |
||
65 | ->addOption(InputOptionKeys::ARCHIVE_ARTEFACTS, null, InputOption::VALUE_REQUIRED, 'Whether or not files should be archived') |
||
66 | ->addOption(InputOptionKeys::SOURCE_DATE_FORMAT, null, InputOption::VALUE_REQUIRED, 'The date format used in the CSV file(s)') |
||
67 | ->addOption(InputOptionKeys::USE_DB_ID, null, InputOption::VALUE_REQUIRED, 'The explicit database ID used for the actual import process') |
||
68 | ->addOption(InputOptionKeys::DB_PDO_DSN, null, InputOption::VALUE_REQUIRED, 'The DSN used to connect to the Magento database where the data has to be imported, e. g. mysql:host=127.0.0.1;dbname=magento;charset=utf8') |
||
69 | ->addOption(InputOptionKeys::DB_USERNAME, null, InputOption::VALUE_REQUIRED, 'The username used to connect to the Magento database') |
||
70 | ->addOption(InputOptionKeys::DB_PASSWORD, null, InputOption::VALUE_REQUIRED, 'The password used to connect to the Magento database') |
||
71 | ->addOption(InputOptionKeys::LOG_LEVEL, null, InputOption::VALUE_REQUIRED, 'The log level to use') |
||
72 | ->addOption(InputOptionKeys::DEBUG_MODE, null, InputOption::VALUE_REQUIRED, 'Whether or not debug mode should be used'); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Return's the container instance. |
||
77 | * |
||
78 | * @return \Symfony\Component\DependencyInjection\ContainerInterface The container instance |
||
79 | */ |
||
80 | protected function getContainer() |
||
84 | |||
85 | /** |
||
86 | * Executes the current command. |
||
87 | * |
||
88 | * This method is not abstract because you can use this class |
||
89 | * as a concrete class. In this case, instead of defining the |
||
90 | * execute() method, you set the code to execute by passing |
||
91 | * a Closure to the setCode() method. |
||
92 | * |
||
93 | * @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
||
94 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
95 | * |
||
96 | * @return null|int null or 0 if everything went fine, or an error code |
||
97 | * @throws \LogicException When this abstract method is not implemented |
||
98 | * @see \Symfony\Component\Console\Command\Command::execute() |
||
99 | */ |
||
100 | protected function execute(InputInterface $input, OutputInterface $output) |
||
105 | } |
||
106 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: