<?php

namespace common\logic\Payment\Event;

use common\logic\Order\Order\Event\OrderDeleteEvent;
use common\logic\Order\Order\Model\Order;
use common\logic\Payment\Module\PaymentModule;
use common\logic\User\User\Module\UserModule;
use justcoded\yii2\eventlistener\observers\Observer;

class OrderObserver extends Observer
{
    public function events()
    {
        return [
            Order::EVENT_DELETE => 'onOrderDelete'
        ];
    }

    public function onOrderDelete(OrderDeleteEvent $event)
    {
        $order = $event->order;
        $paymentManager = PaymentModule::getInstance();
        $userModule = UserModule::getInstance();

        $paymentManager->refundOrderCredit($order, $userModule->getCurrent());
    }
}