1 | <?php |
||
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) |
|
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | 4 | public function getUrl() |
|
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() |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 5 | public function getCustomChannel() |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 6 | public function hasCustomChannel() |
|
99 | } |
||
100 |