1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Watcher\Communicator; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
6
|
|
|
use Coco\SourceWatcher\Utils\i18n; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SlackCommunicator |
10
|
|
|
* @package Coco\SourceWatcher\Watcher\Communicator |
11
|
|
|
*/ |
12
|
|
|
class SlackCommunicator implements Communicator |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected string $webHookUrl = ""; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected string $method = ""; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected string $contentType = ""; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected string $data = ""; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* SlackCommunicator constructor. |
36
|
|
|
*/ |
37
|
|
|
public function __construct () |
38
|
|
|
{ |
39
|
|
|
$this->method = "POST"; |
40
|
|
|
$this->contentType = "Content-Type: application/json"; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getWebHookUrl () : string |
47
|
|
|
{ |
48
|
|
|
return $this->webHookUrl; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $webHookUrl |
53
|
|
|
*/ |
54
|
|
|
public function setWebHookUrl ( string $webHookUrl ) : void |
55
|
|
|
{ |
56
|
|
|
$this->webHookUrl = $webHookUrl; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getMethod () : string |
63
|
|
|
{ |
64
|
|
|
return $this->method; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $method |
69
|
|
|
*/ |
70
|
|
|
public function setMethod ( string $method ) : void |
71
|
|
|
{ |
72
|
|
|
$this->method = $method; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getContentType () : string |
79
|
|
|
{ |
80
|
|
|
return $this->contentType; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $contentType |
85
|
|
|
*/ |
86
|
|
|
public function setContentType ( string $contentType ) : void |
87
|
|
|
{ |
88
|
|
|
$this->contentType = $contentType; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getData () : string |
95
|
|
|
{ |
96
|
|
|
return $this->data; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $data |
101
|
|
|
*/ |
102
|
|
|
public function setData ( string $data ) : void |
103
|
|
|
{ |
104
|
|
|
$this->data = $data; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return bool|string |
109
|
|
|
* @throws SourceWatcherException |
110
|
|
|
*/ |
111
|
|
|
public function send () |
112
|
|
|
{ |
113
|
|
|
if ( empty( $this->webHookUrl ) ) { |
114
|
|
|
throw new SourceWatcherException( i18n::getInstance()->getText( SlackCommunicator::class, "No_Web_Hook" ) ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ( empty( $this->method ) ) { |
118
|
|
|
throw new SourceWatcherException( i18n::getInstance()->getText( SlackCommunicator::class, "No_Method" ) ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ( empty( $this->contentType ) ) { |
122
|
|
|
throw new SourceWatcherException( i18n::getInstance()->getText( SlackCommunicator::class, "No_Content_Type" ) ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ( empty( $this->data ) ) { |
126
|
|
|
throw new SourceWatcherException( i18n::getInstance()->getText( SlackCommunicator::class, "No_Data" ) ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$ch = curl_init( $this->webHookUrl ); |
130
|
|
|
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $this->method ); |
|
|
|
|
131
|
|
|
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( $this->contentType, "Content-Length: " . strlen( $this->data ) ) ); |
132
|
|
|
curl_setopt( $ch, CURLOPT_POSTFIELDS, $this->data ); |
133
|
|
|
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); |
134
|
|
|
|
135
|
|
|
return curl_exec( $ch ); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|