<?php

/**
 * Class SG_Giveaway_Limited
 */
class SG_Giveaway_Limited extends SG_Giveaway {

	/**
	 * @var string
	 */
	protected $type = 'limited';

	/**
	 * Is this Giveaway open for subscriptions?
	 *
	 * @return bool
	 */
	public function is_open_to_subscribe() {
		$entries  = giveasap_get_entries_for( $this->get_id() );
		$number   = count( $entries );
		$settings = $this->get_general_settings();

		$limit    = isset( $settings['sg_limit'] ) ? $settings['sg_limit'] : 0;
		$open     = false;

		if ( $limit && $number < $limit ) {
			$open = true;
		}

		return apply_filters( 'sg_giveaway_open_to_subscribe', $open, $this );;
	}

	/**
	 * Return the reason for a closed giveaway.
	 *
	 * @return string
	 */
	public function get_not_open_reason() {
		$settings = $this->get_general_settings();

		$limit    = isset( $settings['sg_limit'] ) ? $settings['sg_limit'] : 0;

		return sprintf( __( 'This Giveaway was limited to %d subscriber(s). Closed for new sign ups.', 'giveasap' ), $limit );
	}

	/**
	 * Is this Giveaway collecting entries?
	 *
	 * @return mixed
	 */
	public function is_collecting_entries() {
		return apply_filters( 'sg_giveaway_collecting_entries', false, $this );
	}
}