Completed
Pull Request — develop (#726)
by Alejandro
05:51
created

MercureUpdatesGenerator::newVisitUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 1
b 0
f 0
ccs 5
cts 5
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
13
use const JSON_THROW_ON_ERROR;
14
15
final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface
16
{
17
    private const NEW_VISIT_TOPIC = 'https://shlink.io/new_visit';
18
19
    private ShortUrlDataTransformer $transformer;
20
21 1
    public function __construct(array $domainConfig)
22
    {
23 1
        $this->transformer = new ShortUrlDataTransformer($domainConfig);
24
    }
25
26 1
    public function newVisitUpdate(Visit $visit): Update
27
    {
28 1
        return new Update(self::NEW_VISIT_TOPIC, json_encode([
29 1
            'shortUrl' => $this->transformer->transform($visit->getShortUrl()),
30 1
            'visit' => $visit->jsonSerialize(),
31 1
        ], JSON_THROW_ON_ERROR));
32
    }
33
}
34