<?php

/**
 * Provides a global endpoint for access to all Atmail quota functions
 *
 * @package Atmail
 * @subpackage Quota
 * @copyright Copyright (C) 2011 Atmail. All rights reserved.
 * @author Atmail
 */

class Atmail_Quota {

	public $configuration;
	public static $debug = false;

	// ** TYPE CONSTANTS ** //
	const TYPE_DB 		= 'Db';
	const TYPE_IMAP		= 'Imap';
	const TYPE_MAILDIR	= 'Maildir';
	const TYPE_SUBADMIN	= 'Subadmin';
	const ACCOUNT		= 'account';
	const TYPE			= 'type';
	const DATA			= 'data';

	// ** SIZE CONVERSION CONSTANTS ** //
	const BYTES			= 0;
	const KB			= 1;
	const MB			= 2;
	const GB			= 3;

	// ** VALID TYPES ACCEPTED ** //
	public static $validTypes = array(
		self::TYPE_DB,
		self::TYPE_IMAP,
		self::TYPE_MAILDIR,
		self::TYPE_SUBADMIN
	);

	public function __construct($configuration = array())
	{
		// see factory for configuration details
		$this->configuration = $configuration;
	}

	static function log($msg)
	{
		if(self::$debug)
			file_put_contents("php://stderr", $msg . "\n");
	}

	public static function getUsedQuota($configuration = array())
	{
		$atmailQuotaObject = Atmail_Quota_Factory::getInstance($configuration);
		if(!is_object($atmailQuotaObject)) return false;
		return $atmailQuotaObject->_getUsedQuota();
	}
	public static function getTotalQuota($configuration = array())
	{
		$atmailQuotaObject = Atmail_Quota_Factory::getInstance($configuration);
		if(!is_object($atmailQuotaObject)) return false;
		return $atmailQuotaObject->_getTotalQuota();
	}

	public static function toHumanReadable($value, $factor = 0, $labelIt = true, $round = false)
	{
		self::log($value);

		$labels = array(
			'B',
			'KB',
			'MB',
			'GB'
		);
		while(($value / 1024.0) > 1 && $factor++ < 4) $value /= 1024.0;
		self::log($value);

		return ($round ? round($value) : round($value, 2)) . ($labelIt ? ' ' . $labels[$factor] : '');
	}
}