-
Notifications
You must be signed in to change notification settings - Fork 0
/
QiitaTeamSlack.php
65 lines (56 loc) · 1.84 KB
/
QiitaTeamSlack.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
<?php
require 'config/setting.php';
require 'lib/Slack.php';
require 'lib/QiitaTeam.php';
class QiitaTeamSlack {
private $payload;
private $slack;
private $qiitaTeam;
public function __construct($payload) {
if (empty($payload)) {
throw new Exception("Failed to initialize class QiitaTeamSlack");
} else {
$this->payload = $payload;
}
$this->setUp();
return $this;
}
public function send() {
$model = $this->qiitaTeam->getModel();
$action = $this->qiitaTeam->getAction();
$this->slack->username('Qiita:team')
->iconUrl('http://s18.postimg.org/569m8zoc5/qiita_team.jpg');
switch ($model) {
case 'item':
if ($action == 'created') {
$item = $this->qiitaTeam->getItem();
$attachments = $item->attachments;
}
break;
case 'comment':
if ($action == 'created') {
$comment = $qiitaTeam->getComment();
$attachments = $comment->attachments;
}
break;
default:
break;
}
if (!empty($attachments)) {
$this->slack->attachments($attachments);
foreach($this->qiitaTeam->getChannelByCreater() as $channel) {
$this->slack->channel($channel)
->send();
}
}
}
private function setUp() {
try {
$this->slack = new Slack(SLACK_WEBHOOK_URL);
$this->qiitaTeam = new QiitaTeam(json_decode(QIITA_MEMBERS, true), QIITA_BASE_URL);
} catch (Exception $e) {
throw new Exception($e->getMessage() . " in class QiitaTeamSlack");
}
$this->qiitaTeam->set($this->payload);
}
}