6 use Services\Http\IHttpResponse
;
7 use Services\IStatusReporter
;
9 class StatusReporter
implements IStatusReporter
11 private $_messages = array();
15 const ERROR
= 'error';
16 const SUCCESS
= 'success';
17 const EXCEPTION
= 'exception';
19 public function __construct(IHttpResponse
$response) {
20 $this->_response
= $response;
23 public function error($message = null
)
25 if($message) $this->addMessage($message);
26 $this->_type
= self
::ERROR
;
27 $this->_response
->setHeader('Content-Type', 'application/json')
28 ->setBody($this->json())
33 public function success($message = null
)
35 if($message) $this->addMessage($message);
36 $this->_type
= self
::SUCCESS
;
37 $this->_response
->setHeader('Content-Type', 'application/json')
38 ->setBody($this->json())
43 public function addMessage($message)
45 $this->_messages
[] = $message;;
48 //no need to exit here, exceptions stop the program.
49 public static function exception(Exception
$exception)
51 //we'll be instatic context here so I have to do it this way.
52 header('Content-Type: application/json');
53 echo json_encode(array(
54 'status' => self
::EXCEPTION
,
55 'messages' => array($exception->getMessage())));
58 public function json()
62 'status' => $this->_type
,
63 'messages' => $this->_messages
)