|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pageon\SlackWebhookMonolog\Slack; |
|
4
|
|
|
|
|
5
|
|
|
use Pageon\SlackWebhookMonolog\General\Exceptions\InvalidUrlException; |
|
6
|
|
|
use Pageon\SlackWebhookMonolog\General\Url; |
|
7
|
|
|
use Pageon\SlackWebhookMonolog\Slack\Interfaces\ChannelInterface; |
|
8
|
|
|
use Pageon\SlackWebhookMonolog\Slack\Interfaces\WebhookInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Jelmer Prins <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @since 0.1.0 |
|
14
|
|
|
*/ |
|
15
|
|
|
class Webhook implements WebhookInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The url of the webhook. This is provided by slack after creating a webhook. |
|
19
|
|
|
* |
|
20
|
|
|
* @var Url |
|
21
|
|
|
*/ |
|
22
|
|
|
private $url; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* A custom channel to override the default set in slack. |
|
26
|
|
|
* |
|
27
|
|
|
* @var ChannelInterface|null |
|
28
|
|
|
*/ |
|
29
|
|
|
private $customChannel = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Url $url The webhook url provided by slack. |
|
33
|
|
|
* @param ChannelInterface|null $customChannel if no channel is provided the default will be used from the config in slack. |
|
34
|
|
|
*/ |
|
35
|
15 |
|
public function __construct(Url $url, ChannelInterface $customChannel = null) |
|
36
|
|
|
{ |
|
37
|
15 |
|
$this->setUrl($url); |
|
38
|
14 |
|
$this->customChannel = $customChannel; |
|
39
|
14 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
4 |
|
public function getUrl() |
|
45
|
|
|
{ |
|
46
|
4 |
|
return $this->url; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* This wil set the url if it is valid. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Url $url |
|
53
|
|
|
* |
|
54
|
|
|
* @throws InvalidUrlException When it is not a valid webhook url of slack |
|
55
|
|
|
* |
|
56
|
|
|
* @return self |
|
57
|
|
|
*/ |
|
58
|
15 |
|
private function setUrl(Url $url) |
|
59
|
|
|
{ |
|
60
|
15 |
|
$urlValidationRegex = '_https:\/\/hooks.slack.com\/services\/[\w\/]+$_iuS'; |
|
61
|
15 |
|
if (!preg_match($urlValidationRegex, (string) $url)) { |
|
62
|
1 |
|
throw new InvalidUrlException( |
|
63
|
1 |
|
sprintf( |
|
64
|
|
|
'The url: "%s" is not a valid url. |
|
65
|
1 |
|
Slack webhook urls should always start with "https://hooks.slack.com/services/"', |
|
66
|
|
|
$url |
|
67
|
1 |
|
), |
|
68
|
|
|
400 |
|
69
|
1 |
|
); |
|
70
|
|
|
} |
|
71
|
14 |
|
$this->url = $url; |
|
72
|
|
|
|
|
73
|
14 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
3 |
|
public function __toString() |
|
80
|
|
|
{ |
|
81
|
3 |
|
return (string) $this->getUrl(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
5 |
|
public function getCustomChannel() |
|
88
|
|
|
{ |
|
89
|
5 |
|
return $this->customChannel; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
6 |
|
public function hasCustomChannel() |
|
96
|
|
|
{ |
|
97
|
6 |
|
return $this->customChannel !== null; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|