1

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 Vendor\ProductImport\Api\Data;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Magento\Framework\App\State;
use Psr\Log\LoggerInterface;

use Vendor\ProductImport\Api\Data\ProductImportInterface;
use Vendor\ProductImport\Model\Provider\StateProvider;

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 Vendor\ProductImport\Api\Data\ProductImportInterface#0 /var/www/php/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Vendor\\Pro...')
#1 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(170): Magento\Framework\ObjectManager\ObjectManager->get('Vendor\\Pro...')
#2 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(276): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Vendor\\Pro...', NULL, 'productImport', 'Vendor\\Pro...')
#3 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(239): Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument('Vendor\\Pro...', Array, Array)
#4 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(34): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime('Vendor\\Pro...', Array, Array)
#5 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(59): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments('Vendor\\Pro...', Array, Array)
#6 /var/www/php/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Vendor\\Pro...')
#7 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(206): Magento\Framework\ObjectManager\ObjectManager->get('Vendor\\Pro...')
#8 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(182): Magento\Framework\ObjectManager\Factory\AbstractFactory->parseArray(Array)
#9 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(276): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, NULL, Array, 'commands', 'Magento\\Framewo...')
#10 /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(239): Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument('Magento\\Framewo...', Array, Array)
#11 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(34): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime('Magento\\Framewo...', Array, Array)
#12 /var/www/php/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(59): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments('Magento\\Framewo...', Array, Array)
#13 /var/www/php/vendor/magento/framework/ObjectManager/ObjectManager.php(56): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Framewo...', Array)
#14 /var/www/php/vendor/magento/framework/Console/Cli.php(153): Magento\Framework\ObjectManager\ObjectManager->create('Magento\\Framewo...')
#15 /var/www/php/vendor/magento/framework/Console/Cli.php(134): Magento\Framework\Console\Cli->getApplicationCommands()
#16 /var/www/php/vendor/symfony/console/Application.php(1274): Magento\Framework\Console\Cli->getDefaultCommands()
#17 /var/www/php/vendor/symfony/console/Application.php(629): Symfony\Component\Console\Application->init()
#18 /var/www/php/vendor/symfony/console/Application.php(237): Symfony\Component\Console\Application->find('setup:di:compil...')
#19 /var/www/php/vendor/magento/framework/Console/Cli.php(115): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 /var/www/php/vendor/symfony/console/Application.php(149): Magento\Framework\Console\Cli->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 /var/www/php/bin/magento(23): Symfony\Component\Console\Application->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="Magento\Framework\Console\CommandList">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="vendor:importproducts:db" xsi:type="object">Vendor\ProductImport\Api\Data\ProductImportCommand</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 Vendor\ProductImport\Api\Data;

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;
}

1 Answer 1

1

Have you declared an implementation in your di.xml? May be adding there would fix your issue.

Add this to your di.xml

<preference for="Vendor\ProductImport\Api\Data\ProductImportInterface" type="Vendor\ProductImport\Model\ProductImport"/>

And you will need to build your model class:

<?php 
namespace Vendor\ProductImport\Model;

use Vendor\ProductImport\Api\Data\ProductImportInterface;

class ProductImport implements ProductImportInterface
{
}

More details here: https://developer.adobe.com/commerce/php/development/components/api-concepts/

3
  • Yeah, I didnt know what to do with the preference class (like the structure and such). I found a workaround to the original issue(starting the import process), but this will help! I'll try your way tomorrow!
    – PaulM
    Jul 14, 2022 at 23:09
  • 1
    I'll mark it as solution for now.
    – PaulM
    Jul 14, 2022 at 23:10
  • Trying this today, it did not work. Throwing the same error for some reason.
    – PaulM
    Jul 15, 2022 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.