-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlackResponse.php
executable file
·80 lines (67 loc) · 2.15 KB
/
SlackResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace Slackbot;
class SlackResponse {
/**
* Single instance of the SlackResponse object.
* @type SlackResponse
*/
private static $instance = null;
/**
* Singleton constructor to initialize the SlackResponse object.
*/
private function __construct() {
}
/**
* Initializes and returns an instance of the SlackResponse object.
*
* @return SlackResponse
*/
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new SlackResponse();
}
return self::$instance;
}
/**
* Declares the current request to be invalid by issuing the 400 HTTP Status Code.
*
* @param String $message
*/
public function invalidRequest($message = null) {
header('HTTP/1.0 400 Bad Request');
$this->respond(
($message !== null) ? $message : '400 Bad Request'
);
}
/**
* Declares the current request to be unauthorized by issuing the 403 HTTP Status Code.
*
* @param String $message
*/
public function unauthorizedRequest($message = null) {
header('HTTP/1.0 403 Forbidden');
$this->respond(
($message !== null) ? $message : '403 Forbidden'
);
}
/**
* Sends the given response text to the browser and ends the request.
*
* @param string|array $response
*/
public function respond($response) {
$expectedResponse = array('text' => '');
if (is_string($response)) {
$expectedResponse['text'] = $response;
} else if (is_array($response)) {
if (!isset($response['text'])) {
throw new \InvalidArgumentException('Missing "text" response index');
}
$expectedResponse['text'] = $response['text'];
} else {
throw new \InvalidArgumentException('Unknown $response data type');
}
echo json_encode($expectedResponse);
exit();
}
}