1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bitbucket\API\Workspaces; |
4
|
|
|
|
5
|
|
|
use Bitbucket\API\Api; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
class Hooks extends Api |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param string $workspace The workspace ID (slug) or workspace UUID in curly brackets. |
12
|
|
|
* @param array $hookConfiguration The hook configuration |
13
|
|
|
* @return ResponseInterface |
14
|
|
|
* |
15
|
|
|
* @throws \InvalidArgumentException |
16
|
|
|
*/ |
17
|
|
|
public function create($workspace, array $hookConfiguration) |
18
|
|
|
{ |
19
|
|
|
$mandatory = [ |
20
|
|
|
'url' => '', |
21
|
|
|
'active' => true, |
22
|
|
|
'events' => [], |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
$diff = array_diff(array_keys($mandatory), array_keys($hookConfiguration)); |
26
|
|
|
if (count($diff) > 0) { |
27
|
|
|
throw new \InvalidArgumentException('Missing parameters for creating a new webhook.'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (false === array_key_exists('events', $hookConfiguration) || 0 === count($hookConfiguration['events'])) { |
31
|
|
|
throw new \InvalidArgumentException('Missing events for creating a new webhook.'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->getClient()->setApiVersion('2.0')->post( |
35
|
|
|
sprintf('/workspaces/%s/hooks', $workspace), |
36
|
|
|
json_encode($hookConfiguration), |
37
|
|
|
['Content-Type' => 'application/json'] |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $workspace The workspace ID (slug) or workspace UUID in curly brackets. |
43
|
|
|
* @param string $uuid The universally unique identifier of the webhook. |
44
|
|
|
* @param array $hookConfiguration The hook configuration |
45
|
|
|
* @return ResponseInterface |
46
|
|
|
* |
47
|
|
|
* @throws \InvalidArgumentException |
48
|
|
|
*/ |
49
|
|
|
public function update($workspace, $uuid, array $hookConfiguration) |
50
|
|
|
{ |
51
|
|
|
$mandatory = [ |
52
|
|
|
'url' => '', |
53
|
|
|
'active' => true, |
54
|
|
|
'events' => [] |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
$diff = array_diff(array_keys($mandatory), array_keys($hookConfiguration)); |
58
|
|
|
if (count($diff) > 0) { |
59
|
|
|
throw new \InvalidArgumentException('Missing parameters for updating a webhook.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (false === array_key_exists('events', $hookConfiguration) || 0 === count($hookConfiguration['events'])) { |
63
|
|
|
throw new \InvalidArgumentException('Missing events for updating a new webhook.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->getClient()->setApiVersion('2.0')->put( |
67
|
|
|
sprintf('/workspaces/%s/hooks/%s', $workspace, $uuid), |
68
|
|
|
json_encode($hookConfiguration), |
69
|
|
|
['Content-Type' => 'application/json'] |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
|
75
|
|
|
* @param string $workspace The workspace ID (slug) or workspace UUID in curly brackets. |
76
|
|
|
* @return ResponseInterface |
77
|
|
|
*/ |
78
|
|
|
public function all($workspace) |
79
|
|
|
{ |
80
|
|
|
return $this->getClient()->setApiVersion('2.0')->get( |
81
|
|
|
sprintf('/workspaces/%s/hooks', $workspace) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $workspace The workspace ID (slug) or workspace UUID in curly brackets. |
87
|
|
|
* @param string $uuid The universally unique identifier of the webhook. |
88
|
|
|
* @return ResponseInterface |
89
|
|
|
*/ |
90
|
|
|
public function get($workspace, $uuid) |
91
|
|
|
{ |
92
|
|
|
return $this->getClient()->setApiVersion('2.0')->get( |
93
|
|
|
sprintf('/workspaces/%s/hooks/%s', $workspace, $uuid) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $workspace The workspace ID (slug) or workspace UUID in curly brackets. |
99
|
|
|
* @param string $uuid The universally unique identifier of the webhook. |
100
|
|
|
* @return ResponseInterface |
101
|
|
|
*/ |
102
|
|
|
public function delete($workspace, $uuid) |
103
|
|
|
{ |
104
|
|
|
return $this->getClient()->setApiVersion('2.0')->delete( |
105
|
|
|
sprintf('/workspaces/%s/hooks/%s', $workspace, $uuid) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|