AssertApiKeyTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
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 41
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://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
19
/**
20
 * Trait AssertApiKeyTrait
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
trait AssertApiKeyTrait
26
{
27
    use ValueToStringTrait;
28
    use AssertRangeTrait;
29
30
    /**
31
     * Minimum length of API key
32
     *
33
     * @var int
34
     */
35
    private static int $apiKeyMinLength = 20;
36
37
    /**
38
     * Maximum length of API key
39
     *
40
     * @var int
41
     */
42
    private static int $apiKeyMaxLength = 45;
43
44
    /**
45
     * Check value is a syntactically valid API key
46
     *
47
     * @param string $value
48
     * @param string $message
49
     *
50
     * @return void
51
     * @throws InvalidArgumentException
52
     */
53 16
    public static function assertApiKey(string $value, string $message = ''): void
54
    {
55 16
        $len = strlen($value);
56
57 16
        $format  = 0 === strlen($message) ? 'Length of API key (%1$s) must be in the range [%2$s..%3$s]' : $message;
58 16
        $message = sprintf(
59 16
            $format,
60 16
            self::valueToString($value),
61 16
            self::valueToString(self::$apiKeyMinLength),
62 16
            self::valueToString(self::$apiKeyMaxLength)
63 16
        );
64
65 16
        self::assertRange($len, self::$apiKeyMinLength, self::$apiKeyMaxLength, $message);
66
    }
67
}
68