2

We are implementing async operations on our Magento 2 module. Given that the module we are using is Magento_AsynchronousOperations and is only available on Magento 2.2 Enterprise and 2.3 versions, how can we make our code backward compatible with Magento 2.2 Community Edition?

The first issue we are running into is that setup:di:compile fails since we don't have the classes on Magento 2.2 Community

We had two ideas in mind

1 - Use ModuleManager to check if the module is enabled.

By removing all use statements from the class and using the module manager to check if the module is enabled, we built an if-else clause to load the dependencies after the class has been instantiated. e.g.:

<?php

namespace MyModule\Services;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Module\Manager;

class BulkOperation 
{

    const ASYNC_MODULE = "Magento_AsynchronousOperations";

    private $manager;
    private $bulkManagement;
    private $operationFactory;

    public function __construct(
        Manager $manager
    )
    {
        $this->manager = $manager;
    }

    public function canUseAsyncOps()
    {
        if ($this->manager->isEnabled(self::ASYNC_MODULE)) {
            $this->bulkManagement = ObjectManager::getInstance()
                ->get(\Magento\Framework\Bulk\BulkManagementInterface::class);
            $this->operationFactory = ObjectManager::getInstance()
                ->get(\Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory::class);
            return true;
        }
        return false;
    }
}

However, we want to avoid using the object manager.

2 - Use class_exists if checks and create interfaces to be DI'ed

<?php
namespace MyModule\Services;

if (\class_exists('\Magento\Framework\Bulk\BulkManagementInterface')
    && \class_exists('Magento\AsynchronousOperations\Api\Data\OperationInterface')
    && \class_exists('Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory')
) {
    interface CustomBulkInterface extends BulkManagementInterface {}
    interface CustomOperationInterface extends OperationInterface {}
    interface CustomOperationInterfaceFactory extends OperationInterfaceFactory {}
} else {
    interface CustomBulkInterface {}
    interface CustomOperationInterface {}
    interface CustomOperationInterfaceFactory {}
}

class BulkOperation 
{

    private $customBulk;
    private $customOperation;
    private $customOperationInterfaceFactory;

    public function __construct(
        CustomBulkInterface $customBulk,
        CustomOperationInterface $customOperation,
        CustomOperationInterfaceFactory $customOperationInterfaceFactory
    ) {
        $this->customBulk = $customBulk;
        $this->customOperation = $customOperation;
        $this->customOperationInterfaceFactory = $customOperationInterfaceFactory;
    }

}

Does anybody has another possible solution in mind?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.