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

MercureUpdatesGenerator::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 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