|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Slack.com Integration |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Steverobbins |
|
8
|
|
|
* @package Steverobbins_Slack |
|
9
|
|
|
* @author Steve Robbins <[email protected]> |
|
10
|
|
|
* @copyright Copyright 2015 Steve Robbins (http://steverobbins.com) |
|
11
|
|
|
* @license http://creativecommons.org/licenses/by/3.0/deed.en_US Creative Commons Attribution 3.0 Unported License |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Abstract model for Slack api actions |
|
16
|
|
|
* |
|
17
|
|
|
* @SuppressWarnings(PHPMD.NumberOfChildren) |
|
18
|
|
|
*/ |
|
19
|
|
|
class Steverobbins_Slack_Model_Api_Abstract extends Varien_Object |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Make a post request |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $action |
|
25
|
|
|
* @param array $fields |
|
26
|
|
|
* |
|
27
|
|
|
* @return stdClass |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function _post($action, array $fields) |
|
30
|
|
|
{ |
|
31
|
|
|
Mage::helper('slack')->log($fields, null, 'slack.log'); |
|
32
|
|
|
return $this->_request( |
|
33
|
|
|
Mage::getSingleton('slack/config_settings')->getApiUrl() . $this->getMethod() . '.' . $action, |
|
34
|
|
|
array( |
|
35
|
|
|
CURLOPT_POST => count($fields), |
|
36
|
|
|
CURLOPT_POSTFIELDS => http_build_query($fields) |
|
37
|
|
|
) |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Make a curl request |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $url |
|
45
|
|
|
* @param array $options |
|
46
|
|
|
* |
|
47
|
|
|
* @return stdClass |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function _request($url, array $options = array()) |
|
50
|
|
|
{ |
|
51
|
|
|
Mage::helper('slack')->log($url, null, 'slack.log'); |
|
52
|
|
|
$ch = curl_init(); |
|
53
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
54
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
55
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true); |
|
56
|
|
|
foreach ($options as $key => $value) { |
|
57
|
|
|
curl_setopt($ch, $key, $value); |
|
58
|
|
|
} |
|
59
|
|
|
$response = curl_exec($ch); |
|
60
|
|
|
$result = new stdClass; |
|
61
|
|
|
$result->code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
62
|
|
|
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
|
63
|
|
|
curl_close($ch); |
|
64
|
|
|
$result->header = $this->_parseHeader(substr($response, 0, $headerSize)); |
|
65
|
|
|
$responseBody = substr($response, $headerSize); |
|
66
|
|
|
$result->body = Mage::helper('core')->jsonDecode($responseBody); |
|
67
|
|
|
Mage::helper('slack')->log($result, null, 'slack.log'); |
|
68
|
|
|
return $result; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Make the headers received from the curl request more readable |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $rawData |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function _parseHeader($rawData) |
|
79
|
|
|
{ |
|
80
|
|
|
$data = array(); |
|
81
|
|
|
foreach (explode("\n", trim($rawData)) as $line) { |
|
82
|
|
|
$bits = explode(': ', $line); |
|
83
|
|
|
if (count($bits) > 1) { |
|
84
|
|
|
$key = $bits[0]; |
|
85
|
|
|
unset($bits[0]); |
|
86
|
|
|
$data[$key] = trim(implode(': ', $bits)); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
return $data; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* API caller |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $name |
|
96
|
|
|
* @param array $args |
|
97
|
|
|
* |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
public function __call($name, $args) |
|
101
|
|
|
{ |
|
102
|
|
|
switch (substr($name, 0, 3)) { |
|
103
|
|
|
case 'get': |
|
104
|
|
|
case 'set': |
|
105
|
|
|
case 'uns': |
|
106
|
|
|
case 'has': |
|
107
|
|
|
return parent::__call($name, $args); |
|
108
|
|
|
} |
|
109
|
|
|
if (!Mage::getSingleton('slack/config_settings')->isActive()) { |
|
110
|
|
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
$params = $this->_prepareParams(); |
|
113
|
|
|
$result = $this->_post($name, $params); |
|
114
|
|
|
if ($result->code != 200) { |
|
115
|
|
|
Mage::throwException('Failed to connect to Slack API'); |
|
116
|
|
|
} elseif (!$result->body['ok']) { |
|
117
|
|
|
Mage::throwException($result->body['error']); |
|
118
|
|
|
} |
|
119
|
|
|
unset($result->body['ok']); |
|
120
|
|
|
foreach ($result->body as $key => $value) { |
|
121
|
|
|
$this->setData($key, $value); |
|
122
|
|
|
} |
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Prepare parameters to send to API |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function _prepareParams() |
|
132
|
|
|
{ |
|
133
|
|
|
$this->setToken(Mage::getSingleton('slack/config_settings')->getToken()); |
|
134
|
|
|
$this->_convertChannels(); |
|
135
|
|
|
$params = $this->getData(); |
|
136
|
|
|
unset($params['method']); |
|
137
|
|
|
return $params; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Convert channel name into identifier |
|
142
|
|
|
* |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function _convertChannels() |
|
146
|
|
|
{ |
|
147
|
|
|
$channelConfig = Mage::getSingleton('slack/config_settings')->getChannels(); |
|
148
|
|
|
if (is_array($channelConfig)) { |
|
149
|
|
|
$channelConfig = array_flip($channelConfig); |
|
150
|
|
|
} |
|
151
|
|
|
if ($this->hasChannels()) { |
|
152
|
|
|
$channels = $this->getChannels(); |
|
153
|
|
|
if (!is_array($channels)) { |
|
154
|
|
|
$channels = array($channels); |
|
155
|
|
|
} |
|
156
|
|
|
foreach ($channels as $key => $channel) { |
|
157
|
|
|
if (isset($channelConfig[$channel])) { |
|
158
|
|
|
$channels[$key] = $channelConfig[$channel]; |
|
159
|
|
|
} else { |
|
160
|
|
|
unset($channels[$key]); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
$this->setChannels($channels); |
|
164
|
|
|
} |
|
165
|
|
|
if ($this->hasChannel()) { |
|
166
|
|
|
$channel = $this->getChannel(); |
|
167
|
|
|
if (isset($channelConfig[$channel])) { |
|
168
|
|
|
$this->setChannel($channelConfig[$channel]); |
|
169
|
|
|
} else { |
|
170
|
|
|
$this->unsChannel(); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|