Notification Setup Error

We have detected an error in your notification set-up (Event-ID dab39dc24f564ec7bd4628d1305fd03c). Currently, we cannot inform you about inspection progress. Please check that the user 557058:bca11929-8c2d-43f2-8a82-c5416880d395 still has access to your repository or update the API account.

Completed
Push — develop ( a60fd1...b17eb0 )
by Alexandru
02:01 queued 15s
created

Hooks::update()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 4
nc 3
nop 3
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