<?php

namespace wpsolr\core\classes\engines;

use wpsolr\core\classes\WpSolrSchema;

/**
 * Some common methods of all engine clients
 *
 */
trait WPSOLR_Client {

	/**
	 * Dos the search engine have an 'exists' filter?
	 * @return bool
	 */
	public function get_has_exists_filter(): bool {
		return true;
	}

	/**
	 * Hierarchy separator
	 * @return string
	 */
	static public function get_facet_hierarchy_separator(): string {
		return WpSolrSchema::FACET_HIERARCHY_SEPARATOR;
	}

	/**
	 * Generate a cacert file for an index.
	 * Stored in WP root to prevent public http access:  /var/www/html/wpsolr_index_my-index-label_cacert.pem
	 *
	 * @param string $index_label
	 * @param string $folder_full_path
	 *
	 * @return string
	 */
	protected function _get_self_cacert_file( string $index_label, string $folder_full_path ): string {
		if ( empty( trim( $folder_full_path ) ) ) {
			// Ensure get_home_path() is declared.
			require_once ABSPATH . 'wp-admin/includes/file.php';
			$folder_full_path = get_home_path();
		} else {
			if ( ! is_dir( $folder_full_path ) ) {
				throw new \Exception( sprintf( 'Folder %s does not exist.', esc_html( $folder_full_path ) ) );
			}

			$folder_full_path = rtrim( rtrim( $folder_full_path ), '/' ) . '/';
		}


		$file = sprintf( '%swpsolr_index_%s_node_cert.pem', $folder_full_path, $index_label );

		return $file;
	}

}
