Completed
Push — master ( a30f79...cb6756 )
by Alejandro
27s queued 13s
created

MercureUpdatesGenerator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 12
dl 0
loc 33
rs 10
c 1
b 0
f 0
ccs 14
cts 14
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 3 1
A newVisitUpdate() 0 5 1
A __construct() 0 3 1
A newShortUrlVisitUpdate() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\Mercure;
6
7
use Shlinkio\Shlink\Core\Entity\Visit;
8
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
9
use Symfony\Component\Mercure\Update;
10
11
use function json_encode;
12
use function sprintf;
13
14
use const JSON_THROW_ON_ERROR;
15
16
final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface
17
{
18
    private const NEW_VISIT_TOPIC = 'https://shlink.io/new-visit';
19
20
    private ShortUrlDataTransformer $transformer;
21
22 2
    public function __construct(array $domainConfig)
23
    {
24 2
        $this->transformer = new ShortUrlDataTransformer($domainConfig);
25
    }
26
27 1
    public function newVisitUpdate(Visit $visit): Update
28
    {
29 1
        return new Update(self::NEW_VISIT_TOPIC, $this->serialize([
30 1
            'shortUrl' => $this->transformer->transform($visit->getShortUrl()),
31 1
            'visit' => $visit,
32
        ]));
33
    }
34
35 1
    public function newShortUrlVisitUpdate(Visit $visit): Update
36
    {
37 1
        $shortUrl = $visit->getShortUrl();
38 1
        $topic = sprintf('%s/%s', self::NEW_VISIT_TOPIC, $shortUrl->getShortCode());
39
40 1
        return new Update($topic, $this->serialize([
41 1
            'shortUrl' => $this->transformer->transform($visit->getShortUrl()),
42 1
            'visit' => $visit,
43
        ]));
44
    }
45
46 2
    private function serialize(array $data): string
47
    {
48 2
        return json_encode($data, JSON_THROW_ON_ERROR);
49
    }
50
}
51