Your IP : 216.73.217.13


Current Path : /var/www/www.indacotrentino.com/www/app/code/Webkul/BuyButton/Plugin/
Upload File :
Current File : //var/www/www.indacotrentino.com/www/app/code/Webkul/BuyButton/Plugin/CorsSetRoutePlugin.php

<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_BuyButton
 * @author    Webkul
 * @copyright Copyright (c)  Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\BuyButton\Plugin;

use Magento\Webapi\Controller\Rest\Router;

class CorsSetRoutePlugin
{

    /**
     * @var \Magento\Framework\Controller\Router\Route\Factory
     */
    protected $_routeFactory;

    /**
     * @param ModelConfig $config
     * @param \Magento\Framework\Controller\Router\Route\Factory $routeFactory
     */
    public function __construct(\Magento\Framework\Controller\Router\Route\Factory $routeFactory)
    {
        $this->_routeFactory = $routeFactory;
    }

    /**
     * allow pre-flight request method
     *
     * @param Config $subject
     * @return void
     */
    public function aroundMatch(
        Router $subject,
        \Closure $proceed,
        $request
    ) {

        try {
            $originalRoute = $proceed($request);
        } catch (\Magento\Framework\Webapi\Exception $e) {
            $requestHttpMethod = $request->getHttpMethod();
            if ($requestHttpMethod == 'OPTIONS') {
                $route = $this->_routeFactory->createRoute(
                    \Magento\Webapi\Controller\Rest\Router\Route::class,
                    '/V1/cors-allow-path'
                );
    
                $route->setServiceClass(\Webkul\BuyButton\Api\AllowCorsInterface::class)
                    ->setServiceMethod('getCustomResponse')
                    ->setSecure(false)
                    ->setAclResources(['anonymous'])
                    ->setParameters([]);
                return $route;
            } else {
                throw $e;
            }
        }
        return $originalRoute;
    }
}