This repository has been archived by the owner on Jan 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCampfireGateway.php
184 lines (165 loc) · 4.25 KB
/
CampfireGateway.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/*
* This file is part of NotifyMe.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NotifyMeHQ\Adapters\Campfire;
use GuzzleHttp\Client;
use NotifyMeHQ\Contracts\GatewayInterface;
use NotifyMeHQ\Http\GatewayTrait;
use NotifyMeHQ\Http\Response;
use NotifyMeHQ\Support\Arr;
class CampfireGateway implements GatewayInterface
{
use GatewayTrait;
/**
* The api endpoint.
*
* @var string
*/
protected $endpoint = 'https://{domain}.campfirenow.com';
/**
* The allowed message types.
*
* @var string[]
*/
protected $allowedTypeMessages = [
'TextMessage',
'PasteMessage',
'TweetMessage',
'SoundMessage',
];
/**
* The allowed sound types.
*
* @var string[]
*/
protected $allowedSounds = [
'56k',
'bueller',
'crickets',
'dangerzone',
'deeper',
'drama',
'greatjob',
'horn',
'horror',
'inconceivable',
'live',
'loggins',
'noooo',
'nyan',
'ohmy',
'ohyeah',
'pushit',
'rimshot',
'sax',
'secret',
'tada',
'tmyk',
'trombone',
'vuvuzela',
'yeah',
'yodel',
];
/**
* Create a new campfire gateway instance.
*
* @param \GuzzleHttp\Client $client
* @param string[] $config
*
* @return void
*/
public function __construct(Client $client, array $config)
{
$this->client = $client;
$this->config = $config;
}
/**
* Send a notification.
*
* @param string $to
* @param string $message
*
* @return \NotifyMeHQ\Contracts\ResponseInterface
*/
public function notify($to, $message)
{
$type = Arr::get($this->config, 'type', 'TextMessage');
if (!in_array($type, $this->allowedTypeMessages)) {
$type = 'TextMessage';
}
$params = ['type' => $type];
if ($type === 'SoundMessage') {
$params['body'] = in_array($message, $this->allowedSounds) ? $message : 'horn';
} else {
$params['body'] = $message;
}
return $this->send($this->buildUrlFromString("room/{$to}/speak.json"), $params);
}
/**
* Send the notification over the wire.
*
* @param string $url
* @param string[] $params
*
* @return \NotifyMeHQ\Contracts\ResponseInterface
*/
protected function send($url, array $params)
{
$success = false;
$rawResponse = $this->client->post($url, [
'exceptions' => false,
'timeout' => '80',
'connect_timeout' => '30',
'headers' => [
'Authorization' => 'Basic '.base64_encode($this->config['token'].':x'),
'Content-Type' => 'application/json',
],
'json' => ['message' => $params],
]);
switch ($rawResponse->getStatusCode()) {
case 201:
$response = [];
$success = true;
break;
case 400:
$response = ['error' => 'Incorrect request values.'];
break;
case 404:
$response = ['error' => 'Invalid room.'];
break;
default:
$response = $this->responseError($rawResponse);
}
return $this->mapResponse($success, $response);
}
/**
* Map the raw response to our response object.
*
* @param bool $success
* @param array $response
*
* @return \NotifyMeHQ\Contracts\ResponseInterface
*/
protected function mapResponse($success, array $response)
{
return (new Response())->setRaw($response)->map([
'success' => $success,
'message' => $success ? 'Message sent' : $response['error'],
]);
}
/**
* Get the request url.
*
* @return string
*/
protected function getRequestUrl()
{
return str_replace('{domain}', $this->config['from'], $this->endpoint);
}
}