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