Skip to content

Having trouble refactoring Submit Index class that extends deprecated MagentoFrameworkAppActionAction;

<?php

namespace AlexAskQuestionControllerSubmit;

use AlexAskQuestionModelAskQuestionFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkControllerResultJson;
use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkDataFormFormKeyValidator;
use MagentoFrameworkExceptionLocalizedException;

class Index extends Action
{
    public const STATUS_ERROR = 'Error';

    public const STATUS_SUCCESS = 'Success';

    /**
     * @var Validator
     */
    private Validator $formKeyValidator;

    /**
     * @var AskQuestionFactory
     */
    private $askQuestionFactory;

    /**
     * Index constructor
     *
     * @param Validator $formKeyValidator
     * @param AskQuestionFactory $askQuestionFactory
     * @param Context $context
     */
    public function __construct(
        Validator          $formKeyValidator,
        AskQuestionFactory $askQuestionFactory,
        Context            $context
    ) {
        parent::__construct($context);
        $this->formKeyValidator = $formKeyValidator;
        $this->askQuestionFactory = $askQuestionFactory;
    }

    /**
     * @return MagentoFrameworkAppResponseInterface|Json|MagentoFrameworkControllerResultInterface
     */
    public function execute()
    {
        $request = $this->getRequest();

        try {

            if (!$this->formKeyValidator->validate($request) || $request->getParam('hideit')) {
                throw new LocalizedException(__('Something went wrong.
                Probably you were away for quite a long time already.
                Please, reload the page and try again.'));
            }

            if (!$request->isAjax()) {
                throw new LocalizedException(__('This request is not valid and can not be processed.'));
            }

            $askQuestion = $this->askQuestionFactory->create();
            $askQuestion->setName($request->getParam('name'))
                ->setEmail($request->getParam('email'))
                ->setPhone($request->getParam('phone'))
                ->setProductName($request->getParam('product_name'))
                ->setSku($request->getParam('sku'))
                ->setQuestion($request->getParam('question'));
            $askQuestion->save();

            $data = [
                'status' => self::STATUS_SUCCESS,
                'message' => __('Your request was submitted. We'll get in touch with you as soon as possible.')
            ];

        } catch (LocalizedException $e) {
            $data = [
                'status' => self::STATUS_ERROR,
                'message' => $e->getMessage()
            ];
        }

        /**
         * @var Json $controllerResult
         */
        $controllerResult = $this->resultFactory->create(ResultFactory::TYPE_JSON);

        return $controllerResult->setData($data);
    }
}

What is the correct implementation of ActionInterface in this case?