<?php
namespace ModulesAbstract\FluxRest\ResponseHandler\Operation;

class MoveFile extends OperationAbstract
{
	public $path = "";
	public $newpath = "";

	public function handle()
	{
		$basePath = $this->FluXEmulator->getBasePath();
		if (!$basePath) {
			throw new \Exception\IO\Base(sprintf("basePath wasn't defined!"));
		}

		if (!$this->newpath) {
			throw new \Exception\IO\Base(sprintf("newpath wasn't defined!"));
		}

		$newFile = new \SplFileInfo($this->FluXEmulator->getRestrictedBasePath() . $this->newpath);

		if (!\Engine\Dir::check($newFile->getPath())) {
			\Engine\Dir::create($newFile->getPath());
		}

		// \Engine\File::move is too strict with the file permissions and paths and won't work with uTests or various hand-made tests
		if (!rename($basePath . $this->path, $newFile->getPathname())) {
			throw new \Exception\IO\Base(sprintf("Cannot move '%s' to '%s'", $basePath . $this->path, $this->newpath));
		}
	}
}
