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

ShortUrlDataTransformer::buildMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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