Passed
Push — master ( e369d0...7ba2a4 )
by Jonathan
20:51 queued 24s
created

AssertBaseUriTrait::assertBaseUri()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10
cc 3
nc 2
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://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 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
    /**
30
     * @param mixed $value
31
     *
32
     * @return string
33
     */
34
    abstract protected static function valueToString($value): string;
35
36
    /**
37
     * Check value is a known base URI
38
     *
39
     * @param mixed  $value
40
     * @param string $message
41
     */
42 292
    public static function assertBaseUri($value, string $message = ''): void
43
    {
44 292
        $baseUri = ReportingCloud::DEFAULT_BASE_URI;
45
46 292
        $host1 = (string) parse_url($baseUri, PHP_URL_HOST);
47 292
        $host2 = (string) parse_url($value, PHP_URL_HOST);
48
49 292
        if (!StringUtils::endsWith($host2, $host1)) {
50 9
            $format  = $message ?: 'Expected base URI to end in %2$s. Got %1$s';
51 9
            $message = sprintf($format, self::valueToString($value), self::valueToString($host1));
52 9
            throw new InvalidArgumentException($message);
53
        }
54 286
    }
55
}
56