1
|
|
|
<?php |
2
|
|
|
namespace Joesama\Webhook\Connectors; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
5
|
|
|
use GuzzleHttp\Exception\TransferException; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use GuzzleHttp\RequestOptions; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
abstract class AbstractConnector |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Web Hook connector id. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $webHookConnectorId; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Define request content type. |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
1 |
|
public function webHookContentType(): string |
26
|
|
|
{ |
27
|
1 |
|
return RequestOptions::JSON; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Define additional handling HTTP request response. |
32
|
|
|
* |
33
|
|
|
* @param ResponseInterface $response |
34
|
|
|
* @param RequestInterface $request |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function webHookResponse(ResponseInterface $response, RequestInterface $request) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$responseContent = $response->getBody()->getContents(); |
40
|
|
|
|
41
|
|
|
if (($jsonResponse = json_decode($responseContent, true)) === null) { |
42
|
|
|
return $responseContent; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $jsonResponse; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Define additional handling for exceptions. |
50
|
|
|
* |
51
|
|
|
* @param TransferException $exception |
52
|
|
|
* @param RequestInterface $request |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function webHookException(TransferException $exception, RequestInterface $request) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
if ($exception instanceof BadResponseException) { |
58
|
|
|
$response = new Response( |
59
|
|
|
$exception->getResponse()->getStatusCode(), |
60
|
|
|
$exception->getResponse()->getHeaders(), |
61
|
|
|
$exception->getResponse()->getBody()->getContents(), |
62
|
|
|
$exception->getResponse()->getProtocolVersion(), |
63
|
|
|
$exception->getResponse()->getReasonPhrase() |
64
|
|
|
); |
65
|
|
|
} else { |
66
|
|
|
$response = new Response(500, [], $exception->getMessage()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $response; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Save data to data storage. |
74
|
|
|
* |
75
|
|
|
* @param array $logData |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function webHookSavingData(array $logData): void |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
//@TODO: Implement saving logic here.. |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set connector id. |
85
|
|
|
* |
86
|
|
|
* @param string $connectorId |
87
|
|
|
*/ |
88
|
1 |
|
public function setConnectorId(string $connectorId): void |
89
|
|
|
{ |
90
|
1 |
|
$this->webHookConnectorId = $connectorId; |
91
|
1 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set connector id. |
95
|
|
|
* |
96
|
|
|
* @return string|null |
97
|
|
|
*/ |
98
|
1 |
|
public function getConnectorId(): ?string |
99
|
|
|
{ |
100
|
1 |
|
return $this->webHookConnectorId; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.