<?php
declare(strict_types=1);

namespace App\Core\Testing;

use App\Core\Database;

final class TestDatabase extends Database
{
    private int $transactionDepth = 0;

    public function beginTransaction(): void
    {
        $this->transactionDepth++;

        if ($this->transactionDepth === 1) {
            parent::beginTransaction();
        }
    }

    public function commit(): void
    {
        if ($this->transactionDepth > 1) {
            $this->transactionDepth--;
            return;
        }

        $this->transactionDepth = 0;
        parent::commit();
    }

    public function rollBack(): void
    {
        if ($this->transactionDepth > 1) {
            $this->transactionDepth--;
            return;
        }

        $this->transactionDepth = 0;
        parent::rollBack();
    }

    public function forceRollBack(): void
    {
        $this->transactionDepth = 0;
        parent::rollBack();
    }
}
