Passed
Pull Request — master (#7)
by Koen
04:12
created

UrlGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\SPA;
6
7
use Illuminate\Support\Str;
8
use function http_build_query;
9
10
final class UrlGenerator
11
{
12
    private string $baseUrl;
13
14 10
    public function __construct(string $baseUrl)
15
    {
16 10
        $this->baseUrl = $baseUrl;
17 10
    }
18
19 10
    public function to(string $path, array $query = []): string
20
    {
21 10
        $query = array_filter($query);
22
23 10
        if (count($query) === 0) {
24 6
            return $this->format($path);
25
        }
26
27 4
        return $this->format($path) . '?' . http_build_query($query);
28
    }
29
30 10
    private function format(string $path): string
31
    {
32 10
        return $this->baseUrl . (Str::startsWith($path, '/') ? $path : '/' . $path);
33
    }
34
}
35