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

UrlGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 23
ccs 10
cts 10
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A format() 0 3 2
A to() 0 9 2
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