BitbucketService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
eloc 37
dl 0
loc 98
c 1
b 0
f 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPayload() 0 7 2
A getEvent() 0 11 3
A getHeaders() 0 8 1
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * WebhookManager
5
 *
6
 * @author: Luca Agnello <[email protected]>
7
 */
8
9
namespace Gnello\WebhookManager\Services;
10
11
use Gnello\WebhookManager\WebhookManagerException;
12
13
/**
14
 * Class BitbucketService
15
 *
16
 * @link https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html
17
 * @package Gnello\WebhookManager\Services
18
 */
19
class BitbucketService implements ServiceInterface
20
{
21
    /**
22
     * Repository events
23
     */
24
    const PUSH = 'repo:push';
25
    const FORK = 'repo:fork';
26
    const UPDATED = 'repo:updated';
27
    const TRANSFER = 'repo:transfer';
28
    const COMMIT_COMMENT_CREATED = 'repo:commit_comment_created';
29
    const BUILD_STATUS_CREATED = 'repo:commit_status_created';
30
    const BUILD_STATUS_UPDATED = 'repo:commit_status_updated';
31
32
    /**
33
     * Issue events
34
     */
35
    const ISSUE_CREATED = 'issue:created';
36
    const ISSUE_UPDATD = 'issue:updated';
37
    const ISSUE_COMMENT_CREATED = 'issue:comment_created';
38
39
    /**
40
     * Pull request events
41
     */
42
    const PULL_REQUEST_CREATED = 'pullrequest:created';
43
    const PULL_REQUEST_UPDATED = 'pullrequest:updated';
44
    const PULL_REQUEST_APPROVED = 'pullrequest:approved';
45
    const PULL_REQUEST_APPROVAL_REMOVED = 'pullrequest:unapproved';
46
    const PULL_REQUEST_MERGED = 'pullrequest:fulfilled';
47
    const PULL_REQUEST_DECLINED = 'pullrequest:rejected';
48
    const PULL_REQUEST_COMMENT_CREATED = 'pullrequest:comment_created';
49
    const PULL_REQUEST_COMMENT_UPDATED = 'pullrequest:comment_updated';
50
    const PULL_REQUEST_COMMENT_DELETED = 'pullrequest:comment_deleted';
51
52
    /**
53
     * @var array
54
     */
55
    private $options;
56
57
    /**
58
     * @var array
59
     */
60
    private $headers = [];
61
62
    /**
63
     * @var mixed
64
     */
65
    private $payload;
66
67
    /**
68
     * BitbucketService constructor.
69
     *
70
     * @param array $options
71
     */
72
    public function __construct(array $options)
73
    {
74
        $this->options = $options;
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public function getHeaders(): array
81
    {
82
        $this->headers['X-Event-Key'] = $_SERVER['HTTP_X_EVENT_KEY'];
83
        $this->headers['X-Hook-UUID'] = $_SERVER['HTTP_X_HOOK_UUID'];
84
        $this->headers['X-Request-UUID'] = $_SERVER['HTTP_X_REQUEST_UUID'];
85
        $this->headers['X-Attempt-Number'] = $_SERVER['HTTP_X_ATTEMPT_NUMBER'];
86
87
        return $this->headers;
88
    }
89
90
    /**
91
     * @return string
92
     * @throws WebhookManagerException
93
     */
94
    public function getEvent(): string
95
    {
96
        if (empty($this->headers)) {
97
            $this->getHeaders();
98
        }
99
100
        if (!isset($this->headers['X-Event-Key'])) {
101
            throw new WebhookManagerException("No event specified.");
102
        }
103
104
        return $this->headers['X-Event-Key'];
105
    }
106
107
    /**
108
     * @return mixed
109
     */
110
    public function getPayload()
111
    {
112
        if (!isset($this->payload)) {
113
            $this->payload = json_decode(file_get_contents('php://input'), (bool) $this->options['json_decode_assoc']);
114
        }
115
116
        return $this->payload;
117
    }
118
}