Passed
Branch development-2.0 (469a91)
by Jonathan
04:44
created

AssertApiKeyTrait::assertApiKey()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
ccs 9
cts 10
cp 0.9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 1
nop 2
crap 2.004
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP Wrapper
6
 *
7
 * PHP wrapper 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
/**
18
 * Trait AssertApiKeyTrait
19
 *
20
 * @package TxTextControl\ReportingCloud
21
 * @author  Jonathan Maron (@JonathanMaron)
22
 */
23
trait AssertApiKeyTrait
24
{
25
    /**
26
     * Minimum length of API key
27
     *
28
     * @var int
29
     */
30
    private static $apiKeyMinLength = 20;
31
32
    /**
33
     * Maximum length of API key
34
     *
35
     * @var int
36
     */
37
    private static $apiKeyMaxLength = 45;
38
39
    /**
40
     * Validate length of API key
41
     *
42
     * @param string $value
43
     * @param string $message
44
     *
45
     * @return null
46
     * @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException
47
     */
48 6
    public static function assertApiKey(string $value, string $message = '')
49
    {
50 6
        $len = strlen($value);
51
52 6
        $format  = 'Length of API key (%s) must be in the range [%2$s..%3$s]';
53 6
        $message = sprintf(
54
            $message ?:
55 6
            $format,
56 6
            self::valueToString($value),
57 6
            self::valueToString(self::$apiKeyMinLength),
58 6
            self::valueToString(self::$apiKeyMaxLength)
59
        );
60
61 6
        return self::range($len, self::$apiKeyMinLength, self::$apiKeyMaxLength, $message);
62
    }
63
}
64