AssertTimestampTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertTimestamp() 0 11 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 AssertTimestampTrait
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
trait AssertTimestampTrait
26
{
27
    use ValueToStringTrait;
28
    use AssertRangeTrait;
29
30
    /**
31
     * Minimum timestamp (EPOC)
32
     *
33
     * @var int
34
     */
35
    private static int $timestampMin = 0;
36
37
    /**
38
     * Maximum timestamp (PHP_INT_MAX)
39
     *
40
     * @var int
41
     */
42
    private static int $timestampMax = PHP_INT_MAX;
43
44
    /**
45
     * Check value is a valid timestamp
46
     *
47
     * @param int    $value
48
     * @param string $message
49
     *
50
     * @return void
51
     * @throws InvalidArgumentException
52
     */
53 26
    public static function assertTimestamp(int $value, string $message = ''): void
54
    {
55 26
        $format  = 0 === strlen($message) ? 'Timestamp (%1$s) must be in the range [%2$s..%3$s]' : $message;
56 26
        $message = sprintf(
57 26
            $format,
58 26
            self::valueToString($value),
59 26
            self::valueToString(self::$timestampMin),
60 26
            self::valueToString(self::$timestampMax)
61 26
        );
62
63 26
        self::assertRange($value, self::$timestampMin, self::$timestampMax, $message);
64
    }
65
}
66