|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of GitterApi package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Gitter; |
|
9
|
|
|
|
|
10
|
|
|
use Gitter\Resources\AbstractResource; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class WebHook |
|
14
|
|
|
* @package Gitter |
|
15
|
|
|
*/ |
|
16
|
|
|
class WebHook extends AbstractResource |
|
17
|
|
|
{ |
|
18
|
|
|
const HOOK_LEVEL_INFO = 'info'; |
|
19
|
|
|
const HOOK_LEVEL_ERROR = 'error'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $hookId; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $level = self::HOOK_LEVEL_INFO; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $icon = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* WebHook constructor. |
|
38
|
|
|
* @param Client $client |
|
39
|
|
|
* @param string $hookId |
|
40
|
|
|
* @throws \InvalidArgumentException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(Client $client, string $hookId) |
|
43
|
|
|
{ |
|
44
|
|
|
parent::__construct($client); |
|
45
|
|
|
|
|
46
|
|
|
$this->hookId = $hookId; |
|
47
|
|
|
|
|
48
|
|
|
if (!$this->hookId) { |
|
49
|
|
|
throw new \InvalidArgumentException('Invalid Hook Id'); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $level |
|
55
|
|
|
* @return WebHook |
|
56
|
|
|
*/ |
|
57
|
|
|
public function withLevel(string $level): WebHook |
|
58
|
|
|
{ |
|
59
|
|
|
$this->level = $level; |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $message |
|
66
|
|
|
* @return array |
|
67
|
|
|
* @throws \RuntimeException |
|
68
|
|
|
* @throws \InvalidArgumentException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function error(string $message): array |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->withLevel(static::HOOK_LEVEL_ERROR)->send($message); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $message |
|
77
|
|
|
* @return array |
|
78
|
|
|
* @throws \RuntimeException |
|
79
|
|
|
* @throws \InvalidArgumentException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function info(string $message): array |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->withLevel(static::HOOK_LEVEL_INFO)->send($message); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $type |
|
88
|
|
|
* @return $this|WebHook |
|
89
|
|
|
*/ |
|
90
|
|
|
public function withIcon(string $type): WebHook |
|
91
|
|
|
{ |
|
92
|
|
|
$this->icon = $type; |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $message |
|
99
|
|
|
* @return array |
|
100
|
|
|
* @throws \RuntimeException |
|
101
|
|
|
* @throws \InvalidArgumentException |
|
102
|
|
|
*/ |
|
103
|
|
|
public function send(string $message): array |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->fetch($this->buildRoute($message)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $message |
|
110
|
|
|
* @return Route |
|
111
|
|
|
*/ |
|
112
|
|
|
private function buildRoute(string $message): Route |
|
113
|
|
|
{ |
|
114
|
|
|
$icon = $this->level === static::HOOK_LEVEL_ERROR ? 'error' : $this->level; |
|
115
|
|
|
|
|
116
|
|
|
$route = Route::post($this->hookId) |
|
117
|
|
|
->toWebhook() |
|
118
|
|
|
->withBody('message', $message) |
|
119
|
|
|
->withBody('errorLevel', $this->level); |
|
120
|
|
|
|
|
121
|
|
|
if ($this->icon !== null) { |
|
122
|
|
|
$route->withBody('icon', $icon); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $route; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|