AssertBaseUriTrait::assertBaseUri()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP SDK
6
 *
7
 * PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8
 *
9
 * @link      https://www.reporting.cloud to learn more about ReportingCloud
10
 * @link      https://tinyurl.com/vmbbh6kd for the canonical source repository
11
 * @license   https://tinyurl.com/3pc9am89
12
 * @copyright © 2023 Text Control GmbH
13
 */
14
15
namespace TextControl\ReportingCloud\Assert;
16
17
use TextControl\ReportingCloud\Exception\InvalidArgumentException;
18
use TextControl\ReportingCloud\ReportingCloud;
19
20
/**
21
 * Trait AssertBaseUriTrait
22
 */
23
trait AssertBaseUriTrait
24
{
25
    use ValueToStringTrait;
26
27
    /**
28
     * Check value is a known base URI
29
     */
30 76
    public static function assertBaseUri(string $value, string $message = ''): void
31
    {
32 76
        $baseUri = ReportingCloud::DEFAULT_BASE_URI;
33
34 76
        $host1 = parse_url($baseUri, PHP_URL_HOST);
35 76
        $host2 = parse_url($value, PHP_URL_HOST);
36 76
        assert(is_string($host2));
37
38 76
        if (!str_ends_with($host2, $host1)) {
39 4
            $format  = '' === $message ? 'Expected base URI to end in %2$s. Got %1$s' : $message;
40 4
            $message = sprintf($format, self::valueToString($value), self::valueToString($host1));
41 4
            throw new InvalidArgumentException($message);
42
        }
43
    }
44
}
45