<?php
class SubmitPage implements IPage
{

	public static function getInstance()
	{
		return new SubmitPage(new Rest(), new Header(), BusinessLogicProcessor::getInstance());
	}
	private $rest;
	private $header;
	private $businessLogicProcessor;

	private function __construct(Rest $rest, Header $header, BusinessLogicProcessor $businessLogicProcessor)
	{
		$this->rest = $rest;
		$this->header = $header;
		$this->businessLogicProcessor = $businessLogicProcessor;
	}

	public function process()
	{
		try
		{
			$id = $this->rest->get('id');
		}
		catch (RestException $e)
		{
			return "Page ID not specified";
		}
		
		$result = $this->businessLogicProcessor->processSubmit($id);
		
		if(isset($result['error']))
		{
			$_SESSION['error'] = $result;
		}
		
		$redirectUrl = "/";
		if(isset($result['redirect']))
		{
			$redirectUrl = $result['redirect'];
		}
		
		$this->header->location($redirectUrl);
	}
}
