1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace Webhook\Domain\Infrastructure; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Exception\TransferException; |
9
|
|
|
use GuzzleHttp\RequestOptions; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Webhook\Domain\Model\Webhook; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Handler |
15
|
|
|
* |
16
|
|
|
* @package Webhook\Domain\Infrastructure |
17
|
|
|
*/ |
18
|
|
|
final class Handler implements HandlerInterface |
19
|
|
|
{ |
20
|
|
|
/** @var string */ |
21
|
|
|
protected $defaultClient = 'Webhook-Client'; |
22
|
|
|
|
23
|
|
|
/** @var Client|null */ |
24
|
|
|
protected $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Handler constructor. |
28
|
|
|
* |
29
|
|
|
* @param null $client |
30
|
|
|
*/ |
31
|
12 |
View Code Duplication |
public function __construct($client = null) |
|
|
|
|
32
|
|
|
{ |
33
|
12 |
|
if (null === $client) { |
34
|
12 |
|
$client = new Client([ |
35
|
12 |
|
RequestOptions::TIMEOUT => 10, |
36
|
12 |
|
RequestOptions::ALLOW_REDIRECTS => false, |
37
|
|
|
]); |
38
|
|
|
} |
39
|
|
|
|
40
|
12 |
|
$this->client = $client; |
41
|
12 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Webhook $webhook |
45
|
|
|
* |
46
|
|
|
* @return RequestResult |
47
|
|
|
*/ |
48
|
11 |
|
public function handle(Webhook $webhook): RequestResult |
49
|
|
|
{ |
50
|
11 |
|
$result = RequestResult::success(); |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
/** @var ResponseInterface $response */ |
54
|
11 |
|
$response = $this->client->post($webhook->getUrl(), $this->createOptions($webhook)); |
55
|
|
|
|
56
|
9 |
|
if ($response->getStatusCode() !== $webhook->getExpectedCode()) { |
57
|
2 |
|
return RequestResult::codeMissMatch($response->getStatusCode()); |
58
|
|
|
} |
59
|
|
|
|
60
|
7 |
|
if ($webhook->getExpectedContent() !== null |
61
|
7 |
|
&& strpos($response->getBody()->getContents(), $webhook->getExpectedContent()) === false |
62
|
|
|
) { |
63
|
7 |
|
return RequestResult::contentMissMatch(); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
} catch (TransferException $e) { |
67
|
2 |
|
$result = RequestResult::transportError($e->getMessage()); |
68
|
|
|
} |
69
|
|
|
|
70
|
8 |
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Webhook $webhook |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
11 |
|
private function createOptions(Webhook $webhook): array |
79
|
|
|
{ |
80
|
11 |
|
$options = []; |
81
|
|
|
|
82
|
|
|
$headers = [ |
83
|
11 |
|
'Next-Retry' => $webhook->getNextAttempt()->format('U'), |
84
|
11 |
|
'Retry-Count' => $webhook->getAttempt(), |
85
|
11 |
|
'User-Agent' => $webhook->getUserAgent() ?: $this->defaultClient |
86
|
|
|
]; |
87
|
|
|
|
88
|
11 |
|
if ($webhook->isRaw()) { |
89
|
10 |
|
$headers['Content-Type'] = 'application/json'; |
90
|
10 |
|
$options[RequestOptions::BODY] = $webhook->getBody(); |
91
|
|
|
} else { |
92
|
1 |
|
$options[RequestOptions::FORM_PARAMS] = json_decode($webhook->getBody(), true); |
93
|
|
|
} |
94
|
|
|
|
95
|
11 |
|
$options[RequestOptions::HEADERS] = $headers; |
96
|
|
|
|
97
|
11 |
|
return $options; |
98
|
|
|
} |
99
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.