<?php

/**
 * Copyright © Resurs Bank AB. All rights reserved.
 * See LICENSE for license details.
 */

namespace Resursbank\MerchantApi\EventListener\KernelRequest;

use Resursbank\MerchantApi\Service\Ecom;
use Resursbank\MerchantApi\Util\Log;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Throwable;

class ConfigureEcom
{
    public function __construct(
        private readonly Ecom $ecom
    ) {
    }

    /**
     * Configure Ecom.
     *
     * NOTE: This cannot be placed in the constructor since that requires the
     * container (for DI) and our classes haven't been loaded yet at that time.
     */
    public function exec(RequestEvent $event): void
    {
        try {
            $this->ecom->configure();
        } catch (Throwable $error) {
            // @todo Add a flash message in backend.
            Log::error(message: $error);
        }
    }
}
