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 ( fd8cce...90bd51 )
by Gallice
04:31
created

WebhookRequestHandler   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.62%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 21
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 153
ccs 44
cts 47
cp 0.9362
rs 10

9 Methods

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