Completed
Push — master ( 30297a...987919 )
by Alejandro
13s
created

ShortUrlDataTransformer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 18
cts 20
cp 0.9
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 0 23 2
A serializeTag() 0 4 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\Entity\Tag;
9
10
class ShortUrlDataTransformer implements DataTransformerInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    private $domainConfig;
16
17 10
    public function __construct(array $domainConfig)
18
    {
19 10
        $this->domainConfig = $domainConfig;
20 10
    }
21
22
    /**
23
     * @param ShortUrl $value
24
     * @return array
25
     */
26 3
    public function transform($value): array
27
    {
28 3
        $dateCreated = $value->getDateCreated();
29 3
        $longUrl = $value->getLongUrl();
30 3
        $shortCode = $value->getShortCode();
31
32
        return [
33 3
            'shortCode' => $shortCode,
34 3
            'shortUrl' => \sprintf(
35 3
                '%s://%s/%s',
36 3
                $this->domainConfig['schema'] ?? 'http',
37 3
                $this->domainConfig['hostname'] ?? '',
38 3
                $shortCode
39
            ),
40 3
            'longUrl' => $longUrl,
41 3
            'dateCreated' => $dateCreated !== null ? $dateCreated->format(\DateTime::ATOM) : null,
42 3
            'visitsCount' => $value->getVisitsCount(),
43 3
            'tags' => \array_map([$this, 'serializeTag'], $value->getTags()->toArray()),
44
45
            // Deprecated
46 3
            'originalUrl' => $longUrl,
47
        ];
48
    }
49
50
    private function serializeTag(Tag $tag): string
51
    {
52
        return $tag->getName();
0 ignored issues
show
Bug introduced by
Consider using $tag->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
53
    }
54
}
55