SubscriptionPayloadSender::send()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PH\Bundle\CoreBundle\Sender;
6
7
use GuzzleHttp\Client;
8
use JMS\Serializer\SerializationContext;
9
use JMS\Serializer\SerializerInterface;
10
use PH\Component\Core\Model\SubscriptionInterface;
11
use PH\Component\Webhook\Model\WebhookInterface;
12
use Sylius\Component\Resource\Repository\RepositoryInterface;
13
14
final class SubscriptionPayloadSender implements SubscriptionPayloadSenderInterface
15
{
16
    /**
17
     * @var RepositoryInterface
18
     */
19
    private $webhookRepository;
20
21
    /**
22
     * @var SerializerInterface
23
     */
24
    private $serializer;
25
26
    /**
27
     * SubscriptionPayloadSender constructor.
28
     *
29
     * @param RepositoryInterface $webhookRepository
30
     * @param SerializerInterface $serializer
31
     */
32
    public function __construct(RepositoryInterface $webhookRepository, SerializerInterface $serializer)
33
    {
34
        $this->webhookRepository = $webhookRepository;
35
        $this->serializer = $serializer;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function send(SubscriptionInterface $subscription): void
42
    {
43
        $context = new SerializationContext();
44
        $context->setSerializeNull(true);
45
46
        /** @var WebhookInterface[] $destinations */
47
        $destinations = $this->webhookRepository->findAll();
48
49
        $client = new Client();
50
        /** @var WebhookInterface $destination */
51
        foreach ($destinations as $destination) {
52
            $client->request('POST', $destination->getUrl(), [
53
                'body' => $this->serializer->serialize($subscription, 'json', $context),
54
            ]);
55
        }
56
    }
57
}
58