<?php

/**
 * Class WPDesk_Paczkomaty_Open_Ssl
 */
class WPDesk_Paczkomaty_Open_SSL {

	/** @see https://www.openssl.org/docs/man1.1.0/crypto/OPENSSL_VERSION_NUMBER.html */
	const REQUIRED_OPENSSL_VERSION_NUMBER = 0x000906000;
	const REQUIRED_OPENSSL_VERSION = '0.9.6';

	/**
	 * Hooks added by class
	 */
	public function hooks() {
		add_action( 'admin_notices', array( $this, 'admin_notices_action' ) );
	}

	/**
	 * admin_notices action
	 */
	public function admin_notices_action() {
		if ( !$this->is_open_ssl_installed() ) {
			$this->show_admin_notice( __( 'Wtyczka Paczkomaty InPost do prawidłowego działania wymaga biblioteki PHP Open SSL.', 'woocommerce-paczkomaty-inpost' ), 'error' );
		}
		else {
			if ( ! $this->is_open_ssl_installed_in_required_version() ) {
				$this->show_admin_notice(
					sprintf(
						__( 'Wtyczka Paczkomaty InPost do prawidłowego działania wymaga biblioteki PHP Open SSL w wersji nie starszej niż %s.', 'woocommerce-paczkomaty-inpost' ),
						self::REQUIRED_OPENSSL_VERSION)
					, 'error'
				);
			}
		}
	}

	/**
	 * @param string $notice_text
	 * @param string $notice_class
	 */
	private function show_admin_notice( $notice_text, $notice_class ) {
		$class = $notice_class . ' notice';
		echo "<div class=\"$class\"> <p>$notice_text</p></div>";
	}

	/**
	 * Check for Open SSL PHP library is installed
	 *
	 * @return bool
	 */
	private function is_open_ssl_installed() {
		return extension_loaded( 'openssl' );
	}

	/**
	 * Check if installed Open SSL is in required version
	 *
	 * @return bool
	 */
	private function is_open_ssl_installed_in_required_version() {
		return defined( 'OPENSSL_VERSION_NUMBER' ) && OPENSSL_VERSION_NUMBER > self::REQUIRED_OPENSSL_VERSION_NUMBER;
	}

}
