<?php
declare(strict_types=1);

/**
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 * @link          https://cakephp.org CakePHP(tm) Project
 * @since         0.0.1
 * @license       https://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace Cake\ElasticSearch\Datasource;

use Elastica\Exception\ResponseException;

/**
 * Temporary shim for fixtures as they know too much about databases.
 */
class SchemaCollection
{
    /**
     * The connection instance to use.
     *
     * @var \Cake\ElasticSearch\Datasource\Connection
     */
    protected Connection $connection;

    /**
     * Constructor
     *
     * @param \Cake\ElasticSearch\Datasource\Connection $connection The connection instance to use.
     */
    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

    /**
     * Returns an empty array as a shim for fixtures
     *
     * @return array An empty array
     */
    public function listTables(): array
    {
        try {
            $indexes = $this->connection->getDriver()->getStatus()->getIndexNames();
        } catch (ResponseException $e) {
            return [];
        }

        return $indexes;
    }
}
