<?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\Bridge\Symfony\Bundle\Provider\Carrier;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;

final class CarrierTableTruncator
{
    public function truncate(Connection $connection, string $tableName): void
    {
        $platform = $connection->getDatabasePlatform();

        if (!$platform instanceof AbstractMySQLPlatform) {
            $connection->executeStatement($platform->getTruncateTableSql($tableName, true));

            return;
        }

        $connection->executeStatement('SET FOREIGN_KEY_CHECKS=0');

        try {
            $connection->executeStatement($platform->getTruncateTableSql($tableName));
        } finally {
            $connection->executeStatement('SET FOREIGN_KEY_CHECKS=1');
        }
    }
}
