WebhookCallbackListener::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$headers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $headers = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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
}