EmicoEcommerce/Magento2TweakwiseExport

View on GitHub
Controller/Cache/Flush.php

Summary

Maintainability
A
35 mins
Test Coverage
<?php

namespace Tweakwise\Magento2TweakwiseExport\Controller\Cache;

use Tweakwise\Magento2TweakwiseExport\Model\CacheHandler;
use Tweakwise\Magento2TweakwiseExport\Model\Config;
use Tweakwise\Magento2TweakwiseExport\Model\RequestValidator;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\Response\Http;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\App\Response\HttpFactory as ResponseFactory;

/**
 * Class Flush, handle cache flush request from tweakwise platform
 */
class Flush implements ActionInterface
{
    /**
     * @var Context
     */
    protected $context;

    /**
     * @var Config
     */
    protected $config;

    /**
     * @var RequestValidator
     */
    protected $requestValidator;

    /**
     * @var ResponseFactory
     */
    protected $responseFactory;

    /**
     * @var CacheHandler
     */
    protected $cacheHandler;

    /**
     * Flush constructor.
     *
     * @param Context $context
     * @param Config $config
     * @param RequestValidator $requestValidator
     * @param ResponseFactory $responseFactory
     * @param CacheHandler $cacheHandler
     */
    public function __construct(
        Context $context,
        Config $config,
        RequestValidator $requestValidator,
        ResponseFactory $responseFactory,
        CacheHandler $cacheHandler
    ) {
        $this->context = $context;
        $this->requestValidator = $requestValidator;
        $this->config = $config;
        $this->responseFactory = $responseFactory;
        $this->cacheHandler = $cacheHandler;
    }

    /**
     * Execute action based on request and return result
     *
     * Note: Request will be added as operation argument in future
     *
     * @return ResponseInterface
     * @throws NotFoundException
     */
    public function execute(): ResponseInterface
    {
        if (!$this->requestValidator->validateRequestKey($this->context->getRequest())) {
            throw new NotFoundException(__('Page not found.'));
        }

        if (!$this->config->isAllowCacheFlush()) {
            return $this->createOkResponse('Cache flush not enabled in settings');
        }

        // Clear caches
        $this->cacheHandler->clear();

        return $this->createOkResponse('Caches have been flushed');
    }

    /**
     * @param string $body
     * @return Http
     */
    protected function createOkResponse(string $body): Http
    {
        $response = $this->responseFactory->create();
        $response->setNoCacheHeaders();
        $response->setStatusCode(200);
        $response->setBody($body);

        return $response;
    }
}