GithubService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 49
dl 0
loc 122
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getPayload() 0 7 2
A getEvent() 0 11 3
A getHeaders() 0 6 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 GithubService
15
 *
16
 * @link https://developer.github.com/v3/activity/events/types
17
 * @package Gnello\WebhookManager\Services
18
 */
19
class GithubService implements ServiceInterface
20
{
21
    /**
22
     * General events
23
     */
24
    const LABEL = 'label';
25
    const MARKETPLACE_PURCHASE = 'marketplace_purchase';
26
    const MEMBER = 'member';
27
    const MEMBERSHIP = 'membership';
28
    const MILESTONE = 'milestone';
29
    const ORGANIZATION = 'organization';
30
    const ORG_BLOCK = 'org_block';
31
    const PAGE_BUILD = 'page_build';
32
    const PUBLIC = 'public';
33
    const WATCH = 'watch';
34
35
    /**
36
     * Repository events
37
     */
38
    const COMMIT_COMMENT = 'commit_comment';
39
    const CREATE = 'create';
40
    const DELETE = 'delete';
41
    const DEPLOYMENT = 'deployment';
42
    const DEPLOYMENT_STATUS = 'deployment_status';
43
    const FORK = 'fork';
44
    const GOLLUM = 'gollum';
45
    const INSTALLATION = 'installation';
46
    const INSTALLATION_REPOSITORIES = 'installation_repositories';
47
    const PUSH = 'push';
48
    const REPOSITORY = 'repository';
49
    const RELEASE = 'release';
50
    const STATUS = 'status';
51
52
    /**
53
     * Issue events
54
     */
55
    const ISSUE_COMMENT = 'issue_comment';
56
    const ISSUES = 'issues';
57
58
    /**
59
     * Pull request events
60
     */
61
    const PULL_REQUEST_REVIEW_COMMENT = 'pull_request_review_comment';
62
    const PULL_REQUEST_REVIEW = 'pull_request_review';
63
    const PULL_REQUEST = 'pull_request';
64
65
    /**
66
     * Team events
67
     */
68
    const TEAM = 'team';
69
    const TEAM_ADD = 'team_add';
70
71
    /**
72
     * Project events
73
     */
74
    const PROJECT_CARD = 'project_card';
75
    const PROJECT_COLUMN = 'project_column';
76
    const PROJECT = 'project';
77
78
    /**
79
     * @var array
80
     */
81
    private $options;
82
83
    /**
84
     * @var array
85
     */
86
    private $headers = [];
87
88
    /**
89
     * @var mixed
90
     */
91
    private $payload;
92
93
    /**
94
     * BitbucketService constructor.
95
     *
96
     * @param array $options
97
     */
98
    public function __construct(array $options)
99
    {
100
        $this->options = $options;
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getHeaders(): array
107
    {
108
        $this->headers['X-GitHub-Event'] = $_SERVER['HTTP_X_GITHUB_EVENT'];
109
        $this->headers['X-GitHub-Delivery'] = $_SERVER['HTTP_X_GITHUB_DELIVERY'];
110
111
        return $this->headers;
112
    }
113
114
    /**
115
     * @return string
116
     * @throws WebhookManagerException
117
     */
118
    public function getEvent(): string
119
    {
120
        if (empty($this->headers)) {
121
            $this->getHeaders();
122
        }
123
124
        if (!isset($this->headers['X-GitHub-Event'])) {
125
            throw new WebhookManagerException("No event specified.");
126
        }
127
128
        return $this->headers['X-GitHub-Event'];
129
    }
130
131
    /**
132
     * @return mixed
133
     */
134
    public function getPayload()
135
    {
136
        if (!isset($this->payload)) {
137
            $this->payload = json_decode(file_get_contents('php://input'), (bool) $this->options['json_decode_assoc']);
138
        }
139
140
        return $this->payload;
141
    }
142
}