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