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://git.io/Jejj2 for the canonical source repository
11
 * @license   https://git.io/Jejjr
12
 * @copyright © 2022 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Assert;
16
17
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
18
use TxTextControl\ReportingCloud\ReportingCloud;
19
use TxTextControl\ReportingCloud\Stdlib\StringUtils;
20
21
/**
22
 * Trait AssertBaseUriTrait
23
 *
24
 * @package TxTextControl\ReportingCloud
25
 * @author  Jonathan Maron (@JonathanMaron)
26
 */
27
trait AssertBaseUriTrait
28
{
29
    use ValueToStringTrait;
30
31
    /**
32
     * Check value is a known base URI
33
     *
34
     * @param string $value
35
     * @param string $message
36
     */
37 80
    public static function assertBaseUri(string $value, string $message = ''): void
38
    {
39 80
        $baseUri = ReportingCloud::DEFAULT_BASE_URI;
40
41 80
        $host1 = parse_url($baseUri, PHP_URL_HOST);
42 80
        $host2 = parse_url($value, PHP_URL_HOST);
43 80
        assert(is_string($host2));
44
45 80
        if (!StringUtils::endsWith($host2, $host1)) {
46 4
            $format  = 0 === strlen($message) ? 'Expected base URI to end in %2$s. Got %1$s' : $message;
47 4
            $message = sprintf($format, self::valueToString($value), self::valueToString($host1));
48 4
            throw new InvalidArgumentException($message);
49
        }
50
    }
51
}
52