1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Watcher\Communicator; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class SlackCommunicator implements Communicator |
8
|
|
|
{ |
9
|
|
|
protected string $webHookUrl; |
10
|
|
|
protected string $method; |
11
|
|
|
protected string $contentType; |
12
|
|
|
protected string $data; |
13
|
|
|
|
14
|
|
|
public function __construct () |
15
|
|
|
{ |
16
|
|
|
$this->method = "POST"; |
17
|
|
|
$this->contentType = "Content-Type: application/json"; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public function getWebHookUrl () : string |
24
|
|
|
{ |
25
|
|
|
return $this->webHookUrl; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $webHookUrl |
30
|
|
|
*/ |
31
|
|
|
public function setWebHookUrl ( string $webHookUrl ) : void |
32
|
|
|
{ |
33
|
|
|
$this->webHookUrl = $webHookUrl; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function getMethod () : string |
40
|
|
|
{ |
41
|
|
|
return $this->method; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $method |
46
|
|
|
*/ |
47
|
|
|
public function setMethod ( string $method ) : void |
48
|
|
|
{ |
49
|
|
|
$this->method = $method; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getContentType () : string |
56
|
|
|
{ |
57
|
|
|
return $this->contentType; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $contentType |
62
|
|
|
*/ |
63
|
|
|
public function setContentType ( string $contentType ) : void |
64
|
|
|
{ |
65
|
|
|
$this->contentType = $contentType; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getData () : string |
72
|
|
|
{ |
73
|
|
|
return $this->data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $data |
78
|
|
|
*/ |
79
|
|
|
public function setData ( string $data ) : void |
80
|
|
|
{ |
81
|
|
|
$this->data = $data; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return bool|string |
86
|
|
|
* @throws Exception |
87
|
|
|
*/ |
88
|
|
|
public function send () |
89
|
|
|
{ |
90
|
|
|
if ( $this->webHookUrl == null || $this->webHookUrl == "" ) { |
91
|
|
|
throw new Exception( "A web hook url must be defined first." ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ( $this->method == null || $this->method == "" ) { |
95
|
|
|
throw new Exception( "A method must be defined first." ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ( $this->contentType == null || $this->contentType == "" ) { |
99
|
|
|
throw new Exception( "A content type be defined first." ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ( $this->data == null || $this->data == "" ) { |
103
|
|
|
throw new Exception( "Data must be defined first." ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$ch = curl_init( $this->webHookUrl ); |
107
|
|
|
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $this->method ); |
|
|
|
|
108
|
|
|
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( $this->contentType, 'Content-Length: ' . strlen( $this->data ) ) ); |
109
|
|
|
curl_setopt( $ch, CURLOPT_POSTFIELDS, $this->data ); |
110
|
|
|
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); |
111
|
|
|
|
112
|
|
|
return curl_exec( $ch ); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|