<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Cart\LegacyOrderImport;

use Doctrine\DBAL\Connection;
use Symfony\Component\Process\Process;

final class LegacyOrderSourceConnection
{
    public function __construct(
        private readonly Connection $connection,
        private readonly ?Process $sshTunnel = null,
    ) {
    }

    public function connection(): Connection
    {
        return $this->connection;
    }

    public function close(): void
    {
        try {
            $this->connection->close();
        } finally {
            if (null !== $this->sshTunnel && $this->sshTunnel->isRunning()) {
                $this->sshTunnel->stop(2);
            }
        }
    }
}
