Completed
Push — develop ( 8109ce...b15e90 )
by Alejandro
17s queued 12s
created

ListDomainsAction::mapDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Rest\Action\Domain;
6
7
use Laminas\Diactoros\Response\JsonResponse;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
11
use Shlinkio\Shlink\Core\Entity\Domain;
12
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
13
14
use function Functional\map;
15
16
class ListDomainsAction extends AbstractRestAction
17
{
18
    protected const ROUTE_PATH = '/domains';
19
    protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
20
21
    private DomainServiceInterface $domainService;
22
    private string $defaultDomain;
23
24 2
    public function __construct(DomainServiceInterface $domainService, string $defaultDomain)
25
    {
26 2
        $this->domainService = $domainService;
27 2
        $this->defaultDomain = $defaultDomain;
28 2
    }
29
30 2
    public function handle(ServerRequestInterface $request): ResponseInterface
31
    {
32 2
        $regularDomains = $this->domainService->listDomainsWithout($this->defaultDomain);
33
34 2
        return new JsonResponse([
35
            'domains' => [
36
                'data' => [
37 2
                    $this->mapDomain($this->defaultDomain, true),
38 2
                    ...map($regularDomains, fn (Domain $domain) => $this->mapDomain($domain->getAuthority())),
39
                ],
40
            ],
41
        ]);
42
    }
43
44 2
    private function mapDomain(string $domain, bool $isDefault = false): array
45
    {
46
        return [
47 2
            'domain' => $domain,
48 2
            'isDefault' => $isDefault,
49
        ];
50
    }
51
}
52