SubscriptionPayloadSender   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A send() 0 16 2
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