GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 961991...0a93f7 )
by Gallice
03:51
created

WebhookRequestHandler   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 144
ccs 42
cts 45
cp 0.9333
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
B isValid() 0 13 5
A getAllCallbackEvents() 0 10 2
A getEntries() 0 4 1
A getRequest() 0 4 1
A getDecodedBody() 0 10 3
A getHydratedEntries() 0 17 3
A isValidHeader() 0 12 2
1
<?php
2
3
namespace Tgallice\FBMessenger;
4
5
use GuzzleHttp\Psr7\ServerRequest;
6
use Psr\Http\Message\RequestInterface;
7
use Tgallice\FBMessenger\Callback\CallbackEvent;
8
use Tgallice\FBMessenger\Model\Callback\Entry;
9
10
class WebhookRequestHandler
11
{
12
    /**
13
     * @var RequestInterface
14
     */
15
    private $request;
16
17
    /**
18
     * App secret used to verify the request sha1
19
     *
20
     * @var string
21
     */
22
    private $secret;
23
24
    /**
25
     * @var array
26
     */
27
    private $decodedBody;
28
29
    /**
30
     * @var Entry[]
31
     */
32
    private $hydratedEntries;
33
34
    /**
35
     * @var string
36
     */
37
    private $body;
38
39
    /**
40
     * @param string $secret
41
     * @param RequestInterface|null $request
42
     */
43 8
    public function __construct($secret, RequestInterface $request = null)
44
    {
45 8
        $this->secret = $secret;
46 8
        $this->request = null === $request ? ServerRequest::fromGlobals() : $request;
47
48
        // Reset pointer to the beginning
49 8
        $this->request->getBody()->rewind();
50 8
        $this->body = $this->request->getBody()->getContents();
51 8
        $this->request->getBody()->rewind();
52 8
    }
53
54
    /**
55
     * Check if the request is a valid webhook request
56
     *
57
     * @return bool
58
     */
59 3
    public function isValid()
60
    {
61 3
        if (!$this->isValidHeader()) {
62 2
            return false;
63
        }
64
65 1
        $decoded = $this->getDecodedBody();
66
67 1
        $object = isset($decoded['object']) ? $decoded['object'] : null;
68 1
        $entry = isset($decoded['entry']) ? $decoded['entry'] : null;
69
70 1
        return $object === 'page' && null !== $entry;
71
    }
72
73
    /**
74
     * @return CallbackEvent[]
75
     */
76 1
    public function getAllCallbackEvents()
77
    {
78 1
        $events = [];
79
80 1
        foreach ($this->getHydratedEntries() as $hydratedEntry) {
81 1
            $events = array_merge($events, $hydratedEntry->getCallbackEvents());
82 1
        }
83
84 1
        return $events;
85
    }
86
87
    /**
88
     * @return Entry[]
89
     */
90 1
    public function getEntries()
91
    {
92 1
        return $this->getHydratedEntries();
93
    }
94
95
    /**
96
     * @return RequestInterface
97
     */
98 1
    public function getRequest()
99
    {
100 1
        return $this->request;
101
    }
102
103
    /**
104
     * @return array
105
     */
106 4
    public function getDecodedBody()
107
    {
108 4
        if (isset($this->decodedBody)) {
109
            return $this->decodedBody;
110
        }
111
112 4
        $decoded = @json_decode($this->body, true);
113
114 4
        return $this->decodedBody = null === $decoded ? [] : $decoded;
115
    }
116
117
    /**
118
     * @return Entry[]
119
     */
120 2
    private function getHydratedEntries()
121
    {
122 2
        if (isset($this->hydratedEntries)) {
123
            return $this->hydratedEntries;
124
        }
125
126 2
        $decodedBody = $this->getDecodedBody();
127 2
        $entries = $decodedBody['entry'];
128
129 2
        $hydrated = [];
130
131 2
        foreach ($entries as $entry) {
132 2
            $hydrated[] = Entry::create($entry);
133 2
        }
134
135 2
        return $this->hydratedEntries = $hydrated;
136
    }
137
138
    /**
139
     * @return bool
140
     */
141 3
    private function isValidHeader()
142
    {
143 3
        $headers = $this->request->getHeader('X-Hub-Signature');
144
145 3
        if (empty($headers)) {
146
            return false;
147
        }
148
149 3
        $signature = XHubSignature::parseHeader($headers[0]);
150
151 3
        return XHubSignature::validate($this->body, $this->secret, $signature);
152
    }
153
}
154