AssertApiKeyTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 13
c 1
b 0
f 0
dl 0
loc 31
ccs 10
cts 10
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertApiKey() 0 13 2
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
/**
18
 * Trait AssertApiKeyTrait
19
 */
20
trait AssertApiKeyTrait
21
{
22
    use AssertRangeTrait;
23
    use ValueToStringTrait;
24
25
    /**
26
     * Minimum length of API key
27
     */
28
    private static int $apiKeyMinLength = 20;
29
30
    /**
31
     * Maximum length of API key
32
     */
33
    private static int $apiKeyMaxLength = 45;
34
35
    /**
36
     * Check value is a syntactically valid API key
37
     */
38 16
    public static function assertApiKey(string $value, string $message = ''): void
39
    {
40 16
        $len = strlen($value);
41
42 16
        $format  = '' === $message ? 'Length of API key (%1$s) must be in the range [%2$s..%3$s]' : $message;
43 16
        $message = sprintf(
44 16
            $format,
45 16
            self::valueToString($value),
46 16
            self::valueToString(self::$apiKeyMinLength),
47 16
            self::valueToString(self::$apiKeyMaxLength)
48 16
        );
49
50 16
        self::assertRange($len, self::$apiKeyMinLength, self::$apiKeyMaxLength, $message);
51
    }
52
}
53