<?php
/**
 *
 * Adyen Payment module (https://www.adyen.com/)
 *
 * Copyright (c) 2021 Adyen NV (https://www.adyen.com/)
 * See LICENSE.txt for license details.
 *
 * Author: Adyen <magento@adyen.com>
 */

namespace Adyen\Payment\Gateway\Request;

use Adyen\Payment\Helper\BaseUrlHelper;
use Magento\Payment\Gateway\Data\PaymentDataObject;
use Magento\Payment\Gateway\Helper\SubjectReader;
use Magento\Payment\Gateway\Request\BuilderInterface;
use Magento\Sales\Model\Order;

class OriginDataBuilder implements BuilderInterface
{
    /**
     * @var BaseUrlHelper
     */
    private $baseUrlHelper;

    /**
     * CheckoutDataBuilder constructor.
     *
     * @param BaseUrlHelper $baseUrlHelper
     */
    public function __construct(
        BaseUrlHelper $baseUrlHelper
    ) {
        $this->baseUrlHelper = $baseUrlHelper;
    }

    /**
     * @param array $buildSubject
     * @return array
     */
    public function build(array $buildSubject)
    {
        /** @var PaymentDataObject $paymentDataObject */
        $paymentDataObject = SubjectReader::readPayment($buildSubject);
        $payment = $paymentDataObject->getPayment();
        /** @var Order $order */
        $storeId = $payment->getOrder()->getStoreId();

        $request['body']['origin'] = $this->baseUrlHelper->getStoreBaseUrl($storeId);

        return $request;
    }
}
