1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace Webhook\Bundle\EventListener; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Psr7\Request; |
10
|
|
|
use GuzzleHttp\RequestOptions; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
12
|
|
|
use Webhook\Bundle\Event\WebhookEvent; |
13
|
|
|
use Webhook\Bundle\WebhookEvents; |
14
|
|
|
use Webhook\Domain\Model\Webhook; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class WebhookCallbackListener |
18
|
|
|
* |
19
|
|
|
* @package Webhook\Bundle\EventListener |
20
|
|
|
*/ |
21
|
|
|
final class WebhookCallbackListener implements EventSubscriberInterface |
22
|
|
|
{ |
23
|
|
|
/** @var Client */ |
24
|
|
|
private $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* WebhookCallbackListener constructor. |
28
|
|
|
* |
29
|
|
|
* @param Client|null $client |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function __construct(Client $client = null) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
if (null === $client) { |
34
|
|
|
$client = new Client([RequestOptions::TIMEOUT => 10]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->client = $client; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public static function getSubscribedEvents(): array |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
WebhookEvents::WEBHOOK_DONE => 'handle', |
48
|
|
|
WebhookEvents::WEBHOOK_FAIL => 'handle', |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param WebhookEvent $event |
54
|
|
|
*/ |
55
|
|
|
public function handle(WebhookEvent $event) |
56
|
|
|
{ |
57
|
|
|
$webhook = $event->getWebhook(); |
58
|
|
|
|
59
|
|
|
if (null === $webhook->getCallbackUrl()) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$request = $this->createRequest($webhook); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$this->client->send($request); |
67
|
|
|
} catch (\Throwable $exception) { |
68
|
|
|
// just skip :) |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $webhook |
74
|
|
|
* |
75
|
|
|
* @return Request |
76
|
|
|
*/ |
77
|
|
|
private function createRequest(Webhook $webhook): Request |
78
|
|
|
{ |
79
|
|
|
$headers['Content-Type'] = 'application/json'; |
|
|
|
|
80
|
|
|
|
81
|
|
|
$data = [ |
82
|
|
|
'id' => $webhook->getId(), |
83
|
|
|
'status' => $webhook->getStatus(), |
84
|
|
|
'processed' => (int)$webhook->getProcessed()->format('U'), |
85
|
|
|
'metadata' => $webhook->getMetadata(), |
86
|
|
|
'attempt' => $webhook->getAttempt(), |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
return new Request('POST', $webhook->getCallbackUrl(), $headers, json_encode($data)); |
90
|
|
|
} |
91
|
|
|
} |
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.