Passed
Push — master ( fbc447...748ca4 )
by Jonathan
18:21
created

AssertDocumentDividerTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 31
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertDocumentDivider() 0 20 5
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
use ReflectionClass;
18
use ReflectionException;
19
use TxTextControl\ReportingCloud\ReportingCloud;
20
use TxTextControl\ReportingCloud\Stdlib\StringUtils;
21
22
/**
23
 * Trait DocumentDividerTrait
24
 *
25
 * @package TxTextControl\ReportingCloud
26
 * @author  Jonathan Maron (@JonathanMaron)
27
 */
28
trait AssertDocumentDividerTrait
29
{
30
    /**
31
     * Validate a document divider
32
     *
33
     * @param int    $value
34
     * @param string $message
35
     *
36
     * @return null
37
     * @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException
38
     */
39 12
    public static function assertDocumentDivider(int $value, string $message = '')
40
    {
41 12
        $haystack = [];
42
        try {
43 12
            $reportingCloud  = new ReportingCloud();
44 12
            $reflectionClass = new ReflectionClass($reportingCloud);
45 12
            foreach ($reflectionClass->getConstants() as $constantKey => $constantValue) {
46 12
                if (StringUtils::startsWith($constantKey, 'DOCUMENT_DIVIDER_')) {
47 12
                    $haystack[] = $constantValue;
48
                }
49
            }
50 12
            unset($reportingCloud);
51
        } catch (ReflectionException $e) {
52
            // continue
53
        }
54
55 12
        $format  = '%s contains an unsupported document divider';
56 12
        $message = sprintf($message ?: $format, self::valueToString($value));
57
58 12
        return self::oneOf($value, $haystack, $message);
59
    }
60
}
61