Riskified / magento2new

New Magento 2 code refactor branch
1 stars 11 forks source link

Area code is not set error #81

Open fenixn opened 6 months ago

fenixn commented 6 months ago

Magento ver. 2.4.3-p1

Observed behavior when installing extension:

bin/magento setup:upgrade

Updating modules:
Area code is not set

In State.php line 153:

  Area code is not set

Testing shows that this file causes the error: vendor/riskified/magento2new/Model/Command/UploadHistoricalOrders.php

Setting the area code in this method prevents the area, but later causes a "Area code is already set" error.

    /**
     * @inheritdoc
     */
    protected function configure()
    {
        $this->setName('riskified:sync:historical-orders');
        $this->setDescription('Send your historical orders to riskified backed');

        parent::configure();
    }

I had to set up a patch extension to resolve this error and get Riskified to work. The configure and execute method had to be modified so area code is set for the configure method when it is required, but also prevent setAreaCode from occurring twice.

<?php
namespace Meltontackle\RiskifiedPatch\Model\Command;

use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Riskified\Common\Riskified;
use Riskified\Common\Validations;
use Riskified\Common\Signature;
use Riskified\OrderWebhook\Model;
use Riskified\OrderWebhook\Transport\CurlTransport;
use Riskified\Decider\Model\Api\Order\Helper;

use Riskified\Decider\Model\Command\UploadHistoricalOrders as RiskifiedUploadHistoricalOrders;

class UploadHistoricalOrders extends RiskifiedUploadHistoricalOrders
{
    /**
     * This property is private in original, so has to be redeclared.
     * 
     * @var State
     */
    private $state;

    /**
     * UploadHistoricalOrders constructor.
     * 
     * This method calls parent::__construct in the original.
     * This has to be modified to target that parent.
     *
     * @param State $state
     * @param ScopeConfigInterface $scopeConfig
     * @param OrderRepositoryInterface $orderRepository
     * @param SearchCriteria $searchCriteriaBuilder
     * @param Helper $helper
     */
    public function __construct(
        State $state,
        ScopeConfigInterface $scopeConfig,
        OrderRepositoryInterface $orderRepository,
        SearchCriteria $searchCriteriaBuilder,
        Helper $helper
    ) {
        $this->_scopeConfig             = $scopeConfig;
        $this->_orderRepository         = $orderRepository;
        $this->_searchCriteriaBuilder   = $searchCriteriaBuilder;

        $this->_orderHelper = $helper;

        $this->state = $state;

        $this->_transport = new CurlTransport(new Signature\HttpDataSignature());
        $this->_transport->timeout = 15;

        // Begin modify
        Command::__construct();
        // End modify
    }

    /**
     * Modified to set area code if not already set.
     * 
     * @inheritdoc
     */
    protected function configure()
    {
        // Begin modify
        try {
            $this->state->getAreaCode();
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            try {
                $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                // Area code is already set
            }
        } 
        // End modify

        $this->setName('riskified:sync:historical-orders');
        $this->setDescription('Send your historical orders to riskified backed');

        parent::configure();
    }

    /**
     * Modified to prevent 'Area code is already set' error if configure method has set the area code.
     * 
     * @inheritdoc
     * @throws LocalizedException
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // Begin modify
        try {
            $this->state->getAreaCode();
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            try {
                $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                // Area code is already set
            }
        } 
        // End modify

        $authToken = $this->_scopeConfig->getValue('riskified/riskified/key', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        $env = constant('\Riskified\Common\Env::' . $this->_scopeConfig->getValue('riskified/riskified/env'));
        $domain = $this->_scopeConfig->getValue('riskified/riskified/domain');

        $output->writeln("Riskified auth token: $authToken \n");
        $output->writeln("Riskified shop domain: $domain \n");
        $output->writeln("Riskified target environment: $env \n");
        $output->writeln("*********** \n");

        Riskified::init($domain, $authToken, $env, Validations::SKIP);

        $fullOrderRepository = $this->getEntireCollection();
        $total_count = $fullOrderRepository->getSize();

        $output->writeln("Starting to upload orders, total_count: $total_count \n");
        $this->getCollection();
        while ($this->_totalUploaded < $total_count) {
            try {
                $this->postOrders();
                $this->_totalUploaded += count($this->_orders);
                $this->_currentPage++;
                $output->writeln("Uploaded " .
                    $this->_totalUploaded .
                    " of " .
                    $total_count
                    ." orders\n");

                $this->getCollection();
            } catch (\Exception $e) {
                $output->writeln("<error>".$e->getMessage()."</error> \n");
                exit(1);
            }
        }
    }
}

I'm looking for this issue to be fixed so I can remove my patch extension.

fenixn commented 6 months ago

So setting the area code in the configure of vendor/riskified/magento2new/Model/Command/UploadHistoricalOrders.php allowed me to install the extension but it caused issues with other extensions.

Further testing shows that the file causing this error is actually: vendor/riskified/magento2new/Model/Command/ReleaseOnHoldOrders.php

Tried disabling all extensions with Console Command to track it down. It is always the Riskified extension that causes the error.

I then tried disabling each command in the di.xml file vendor/riskified/magento2new/etc/di.xml

Riskified\Decider\Model\Command\UploadHistoricalOrders

When Riskified\Decider\Model\Command\ReleaseOnHoldOrders is commented out, everything works as it should.