<?php


namespace Modules\Utils\ExportData;

/**
 * The (export data) webservice confirm file request. The request also has a method for validating
 * that the requested file info are the same as the first file from the queue.
 * @author Gabriel
 */
class Ws_ConfirmFile_Request
{

	/**
	 * The first file name.
	 * @var String
	 */
	public $fileName;

	/**
	 * The first file size.
	 * @var Int
	 */
	public $fileSize;


	/**
	 * Checks the info of the first file from the queue with the ones provided during request.
	 * @return Boolean - true if name and size are the same.
	 */
	public function validate()
	{

		global $CFG;

		// get the first file from the queue
		$queueContent = \App\ExportData\QueueManager::getInstance()->getFirstFiles(1);


		// if no file in the queue
		if (!$queueContent) {
			return false;
		}

		// gets the file name
		$fileName = reset($queueContent);
		// check file name
		if ($fileName!=$this->fileName) {
			return false;
		}

		// check the file size
		$fileNameWithPath = $CFG['exportData']['queuePath'].$fileName;
		if (filesize($fileNameWithPath)!=$this->fileSize) {
			return false;
		}

		return true;

	}

}