<?php
/**
 *
 * Adyen Payment Module
 *
 * Copyright (c) 2020 Adyen B.V.
 * This file is open source and available under the MIT license.
 * See the LICENSE file for more info.
 *
 * Author: Adyen <magento@adyen.com>
 */

namespace Adyen\Payment\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Adyen\Payment\Helper\Config;

class AdyenSalesOrderChargedCurrencyObserver implements ObserverInterface
{

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

    public function __construct(
        Config $config
    ) {
        $this->config = $config;
    }

    public function execute(Observer $observer)
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $observer->getEvent()->getOrder();
        $paymentMethod = $order->getPayment()->getMethod();

        if (is_null($paymentMethod)) {
            return;
        }

        if (strpos($paymentMethod, 'adyen_') !== false) {
            $order->setAdyenChargedCurrency($this->config->getChargedCurrency($order->getStoreId()));
        }
    }
}
