Passed
Push — master ( 315e77...e369d0 )
by Jonathan
16:56
created

AssertRangeTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 31
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertRange() 0 11 4
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
19
/**
20
 * Trait AssertRangeTrait
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
trait AssertRangeTrait
26
{
27
    /**
28
     * @param mixed $value
29
     *
30
     * @return string
31
     */
32
    abstract protected static function valueToString($value): string;
33
34
    /**
35
     * Check value is in range min..max
36
     *
37
     * @param int    $value
38
     * @param int    $min
39
     * @param int    $max
40
     * @param string $message
41
     *
42
     * @return void
43
     * @throws InvalidArgumentException
44
     */
45 105
    public static function assertRange(int $value, int $min, int $max, string $message = ''): void
46
    {
47 105
        if ($value < $min || $value > $max) {
48 45
            $format  = $message ?: 'Expected a value between %2$s and %3$s. Got: %1$s';
49 45
            $message = sprintf(
50 45
                $format,
51 45
                self::valueToString($value),
52 45
                self::valueToString($min),
53 45
                self::valueToString($max)
54
            );
55 45
            throw new InvalidArgumentException($message);
56
        }
57 66
    }
58
}
59