I’m working on a command line start of an import process.
The process itself is already a custom plugin. It uses a cron job to run the import with some custom rules.
Here is my command class:
<?php
declare(strict_types = 1);
namespace VendorProductImportApiData;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentConsoleQuestionConfirmationQuestion;
use MagentoFrameworkAppState;
use PsrLogLoggerInterface;
use VendorProductImportApiDataProductImportInterface;
use VendorProductImportModelProviderStateProvider;
class ProductImportCommand extends Command
{
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var State
*/
protected $state;
/**
* @var ProductImportInterface
*/
protected $productImport;
/**
* @var StateProvider
*/
protected $importStateProvider;
/**
* @param State $state
* @param LoggerInterface $logger
* @param ProductImportInterface $productImport
* @param StateProvider $importStateProvider
*/
public function __construct(
State $state,
LoggerInterface $logger,
ProductImportInterface $productImport,
StateProvider $importStateProvider
) {
parent::__construct();
$this->state = $state;
$this->logger = $logger;
$this->productImport = $productImport;
$this->importStateProvider = $importStateProvider;
$this->importStateProvider->setVendorImportState(true);
}
/**
* @inheritDoc
*/
protected function configure():void
{
$this
->setName('vendor:importproducts:db')
->setDescription('Start import process manually.')
->addOption(
'dry-run',
null,
InputOption::VALUE_NONE,
'Perform a dry-run without launching the import.'
);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|void|null
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->state->setAreaCode("adminhtml");
$isDryRun = $input->getOption('dry-run');
if (!$isDryRun) {
$output->writeln(
'Warning: this is not a dry run. If you want to do a dry-run, add --dry-run.'
);
$question = new ConfirmationQuestion('<info>Are you sure you want to continue? [No] </info>', false);
$questionHelper = $this->getHelper('question');
if (!$questionHelper->ask($input, $output, $question))
{
return;
}
}
if (!$isDryRun) {
try {
$this->productImport->loadData();
} catch (Exception $e) {
$this->logger->critical($e->getMessage());
}
}
}
}
When I try to compile, I get this:
Cannot instantiate interface VendorProductImportApiDataProductImportInterface#0 /var/www/php/vendor/magento/framework/ObjectManager/ObjectManager.php(70): MagentoFrameworkObjectManagerFactoryDynamicDeveloper->create('Vendor\Pro...')
#1 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(170): MagentoFrameworkObjectManagerObjectManager->get('Vendor\Pro...')
#2 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(276): MagentoFrameworkObjectManagerFactoryAbstractFactory->resolveArgument(Array, 'Vendor\Pro...', NULL, 'productImport', 'Vendor\Pro...')
#3 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(239): MagentoFrameworkObjectManagerFactoryAbstractFactory->getResolvedArgument('Vendor\Pro...', Array, Array)
#4 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(34): MagentoFrameworkObjectManagerFactoryAbstractFactory->resolveArgumentsInRuntime('Vendor\Pro...', Array, Array)
#5 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(59): MagentoFrameworkObjectManagerFactoryDynamicDeveloper->_resolveArguments('Vendor\Pro...', Array, Array)
#6 /var/www/php/vendor/magento/framework/ObjectManager/ObjectManager.php(70): MagentoFrameworkObjectManagerFactoryDynamicDeveloper->create('Vendor\Pro...')
#7 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(206): MagentoFrameworkObjectManagerObjectManager->get('Vendor\Pro...')
#8 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(182): MagentoFrameworkObjectManagerFactoryAbstractFactory->parseArray(Array)
#9 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(276): MagentoFrameworkObjectManagerFactoryAbstractFactory->resolveArgument(Array, NULL, Array, 'commands', 'Magento\Framewo...')
#10 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(239): MagentoFrameworkObjectManagerFactoryAbstractFactory->getResolvedArgument('Magento\Framewo...', Array, Array)
#11 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(34): MagentoFrameworkObjectManagerFactoryAbstractFactory->resolveArgumentsInRuntime('Magento\Framewo...', Array, Array)
#12 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(59): MagentoFrameworkObjectManagerFactoryDynamicDeveloper->_resolveArguments('Magento\Framewo...', Array, Array)
#13 /var/www/php/vendor/magento/framework/ObjectManager/ObjectManager.php(56): MagentoFrameworkObjectManagerFactoryDynamicDeveloper->create('Magento\Framewo...', Array)
#14 /var/www/php/vendor/magento/framework/Console/Cli.php(153): MagentoFrameworkObjectManagerObjectManager->create('Magento\Framewo...')
#15 /var/www/php/vendor/magento/framework/Console/Cli.php(134): MagentoFrameworkConsoleCli->getApplicationCommands()
#16 /var/www/php/vendor/symfony/console/Application.php(1274): MagentoFrameworkConsoleCli->getDefaultCommands()
#17 /var/www/php/vendor/symfony/console/Application.php(629): SymfonyComponentConsoleApplication->init()
#18 /var/www/php/vendor/symfony/console/Application.php(237): SymfonyComponentConsoleApplication->find('setup:di:compil...')
#19 /var/www/php/vendor/magento/framework/Console/Cli.php(115): SymfonyComponentConsoleApplication->doRun(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#20 /var/www/php/vendor/symfony/console/Application.php(149): MagentoFrameworkConsoleCli->doRun(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#21 /var/www/php/bin/magento(23): SymfonyComponentConsoleApplication->run()
#22 {main}
My di.xml looks somewhat like this:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoFrameworkConsoleCommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="vendor:importproducts:db" xsi:type="object">VendorProductImportApiDataProductImportCommand</item>
</argument>
</arguments>
</type>
</config>
The weird part about it is that there are TONS of other classes in the plugin using this ProductImportInterface, but none of them have this issue.
Could it be because I’m using Syphony classes and it somehow doesnt see this ProductInterface?
I’m pretty much using the same code Adobe has outlined for a CLI command:
https://developer.adobe.com/commerce/php/development/cli-commands/custom/
UPDATE:
The interface file looks like this:
<?php
declare(strict_types = 1);
namespace VendorProductImportApiData;
interface ProductImportInterface
{
/**
* @return array
*/
public function getConfig(): array;
/**
* @return bool
*/
public function loadData(): bool;
/**
* @param string $fileName
* @return bool
*/
public function saveFile(string $fileName): bool;
}