Response::getMessage()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
3
namespace HelePartnerSyncApi\Response;
4
5
use HelePartnerSyncApi\Client;
6
7
abstract class Response
8
{
9
10
	/**
11
	 * @var string
12
	 */
13
	private $secret;
14
15
	/**
16
	 * @param string $secret
17
	 */
18
	public function __construct($secret)
19
	{
20
		$this->secret = $secret;
21
	}
22
23
	public function render()
24
	{
25
		$response = json_encode(array(
26
			'success' => $this->isSuccessful(),
27
			'message' => $this->getMessage(),
28
			'data' => $this->getData(),
29
		), JSON_PRETTY_PRINT);
30
31
		$signature = hash_hmac(Client::SIGNATURE_ALGORITHM, $response, $this->secret);
32
33
		$this->send($signature, $response);
34
	}
35
36
	/**
37
	 * @return mixed
38
	 */
39
	abstract public function getData();
40
41
	/**
42
	 * @return bool
43
	 */
44
	abstract public function isSuccessful();
45
46
	/**
47
	 * @return int
48
	 */
49
	abstract public function getHttpCode();
50
51
	/**
52
	 * @return string
53
	 */
54
	abstract public function getMessage();
55
56
	/**
57
	 * @param string $signature
58
	 * @param string $response
59
	 */
60
	private function send($signature, $response)
61
	{
62
		header('HTTP/1.1 ' . $this->getHttpCode());
63
		header('Content-Type: application/json');
64
		header(Client::HEADER_SIGNATURE . ': ' . $signature);
65
		header(Client::HEADER_SIGNATURE_ALGORITHM . ': ' . Client::SIGNATURE_ALGORITHM);
66
67
		echo $response;
68
	}
69
70
}
71