Completed
Push — master ( 35950a...014eb2 )
by Alejandro
25s queued 11s
created

ShortUrlDataTransformer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 21
c 2
b 0
f 0
dl 0
loc 44
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 16 1
A buildMeta() 0 10 1
A __construct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Transformer;
5
6
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
7
use Shlinkio\Shlink\Core\Entity\ShortUrl;
8
use Shlinkio\Shlink\Core\Util\ShortUrlBuilderTrait;
9
10
use function Functional\invoke;
11
use function Functional\invoke_if;
12
13
class ShortUrlDataTransformer implements DataTransformerInterface
14
{
15
    use ShortUrlBuilderTrait;
16
17
    /** @var array */
18
    private $domainConfig;
19
20 17
    public function __construct(array $domainConfig)
21
    {
22 17
        $this->domainConfig = $domainConfig;
23
    }
24
25
    /**
26
     * @param ShortUrl $value
27
     */
28 10
    public function transform($value): array
29
    {
30 10
        $longUrl = $value->getLongUrl();
31 10
        $shortCode = $value->getShortCode();
32
33
        return [
34 10
            'shortCode' => $shortCode,
35 10
            'shortUrl' => $this->buildShortUrl($this->domainConfig, $shortCode),
36 10
            'longUrl' => $longUrl,
37 10
            'dateCreated' => $value->getDateCreated()->toAtomString(),
38 10
            'visitsCount' => $value->getVisitsCount(),
39 10
            'tags' => invoke($value->getTags(), '__toString'),
40 10
            'meta' => $this->buildMeta($value),
41
42
            // Deprecated
43 10
            'originalUrl' => $longUrl,
44
        ];
45
    }
46
47 10
    private function buildMeta(ShortUrl $shortUrl): array
48
    {
49 10
        $validSince = $shortUrl->getValidSince();
50 10
        $validUntil = $shortUrl->getValidUntil();
51 10
        $maxVisits = $shortUrl->getMaxVisits();
52
53
        return [
54 10
            'validSince' => invoke_if($validSince, 'toAtomString'),
55 10
            'validUntil' => invoke_if($validUntil, 'toAtomString'),
56 10
            'maxVisits' => $maxVisits,
57
        ];
58
    }
59
}
60