Handler.php 4.42 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Signifyd\Controller\Webhooks;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Signifyd\Api\CaseRepositoryInterface;
use Magento\Signifyd\Model\CaseServices\UpdatingServiceFactory;
use Magento\Signifyd\Model\Config;
use Magento\Signifyd\Model\SignifydGateway\Response\WebhookMessageReader;
use Magento\Signifyd\Model\SignifydGateway\Response\WebhookRequest;
use Magento\Signifyd\Model\SignifydGateway\Response\WebhookRequestValidator;
use Psr\Log\LoggerInterface;

/**
 * Responsible for handling webhook posts from Signifyd service.
 *
 * @see https://www.signifyd.com/docs/api/#/reference/webhooks/
 */
class Handler extends Action implements \Magento\Framework\App\CsrfAwareActionInterface
{
    /**
     * Event topic of test webhook request.
     *
     * @var string
     */
    private static $eventTopicTest = 'cases/test';

    /**
     * @var WebhookRequest
     */
    private $webhookRequest;

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

    /**
     * @var WebhookMessageReader
     */
    private $webhookMessageReader;

    /**
     * @var UpdatingServiceFactory
     */
    private $caseUpdatingServiceFactory;

    /**
     * @var WebhookRequestValidator
     */
    private $webhookRequestValidator;

    /**
     * @var CaseRepositoryInterface
     */
    private $caseRepository;

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

    /**
     * @param Context $context
     * @param WebhookRequest $webhookRequest
     * @param LoggerInterface $logger
     * @param WebhookMessageReader $webhookMessageReader
     * @param UpdatingServiceFactory $caseUpdatingServiceFactory
     * @param WebhookRequestValidator $webhookRequestValidator
     * @param CaseRepositoryInterface $caseRepository
     * @param Config $config
     */
    public function __construct(
        Context $context,
        WebhookRequest $webhookRequest,
        LoggerInterface $logger,
        WebhookMessageReader $webhookMessageReader,
        UpdatingServiceFactory $caseUpdatingServiceFactory,
        WebhookRequestValidator $webhookRequestValidator,
        CaseRepositoryInterface $caseRepository,
        Config $config
    ) {
        parent::__construct($context);
        $this->webhookRequest = $webhookRequest;
        $this->logger = $logger;
        $this->webhookMessageReader = $webhookMessageReader;
        $this->caseUpdatingServiceFactory = $caseUpdatingServiceFactory;
        $this->webhookRequestValidator = $webhookRequestValidator;
        $this->caseRepository = $caseRepository;
        $this->config = $config;
    }

    /**
     * Processes webhook request data and updates case entity
     *
     * @return void
     */
    public function execute()
    {
        if ($this->config->isDebugModeEnabled()) {
            $this->logger->debug($this->webhookRequest->getEventTopic() . '|' . $this->webhookRequest->getBody());
        }

        if (!$this->webhookRequestValidator->validate($this->webhookRequest)) {
            $this->_redirect('noroute');
            return;
        }

        $webhookMessage = $this->webhookMessageReader->read($this->webhookRequest);
        if ($webhookMessage->getEventTopic() === self::$eventTopicTest) {
            return;
        }

        $data = $webhookMessage->getData();
        if (empty($data['caseId'])) {
            $this->_redirect('noroute');
            return;
        }

        $case = $this->caseRepository->getByCaseId($data['caseId']);
        if ($case === null) {
            $this->_redirect('noroute');
            return;
        }

        $caseUpdatingService = $this->caseUpdatingServiceFactory->create($webhookMessage->getEventTopic());
        try {
            $caseUpdatingService->update($case, $data);
        } catch (LocalizedException $e) {
            $this->getResponse()->setHttpResponseCode(400);
            $this->logger->critical($e);
        }
    }

    /**
     * @inheritDoc
     */
    public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
    {
        return null;
    }

    /**
     * @inheritDoc
     */
    public function validateForCsrf(RequestInterface $request): ?bool
    {
        return true;
    }
}