Passed
Branch static-analysis (3fe576)
by Jonathan
19:22
created

AssertDocumentDividerTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 5
eloc 19
dl 0
loc 48
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDocumentDividers() 0 23 3
A assertDocumentDivider() 0 7 2
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\Exception\InvalidArgumentException;
20
use TxTextControl\ReportingCloud\ReportingCloud;
21
use TxTextControl\ReportingCloud\Stdlib\StringUtils;
22
23
/**
24
 * Trait DocumentDividerTrait
25
 *
26
 * @package TxTextControl\ReportingCloud
27
 * @author  Jonathan Maron (@JonathanMaron)
28
 */
29
trait AssertDocumentDividerTrait
30
{
31
    /**
32
     * Validate a document divider
33
     *
34
     * @param int    $value
35
     * @param string $message
36
     *
37
     * @return void
38
     * @throws InvalidArgumentException
39
     */
40 12
    public static function assertDocumentDivider(int $value, string $message = ''): void
41
    {
42 12
        $haystack = self::getDocumentDividers();
43 12
        $format   = $message ?: '"%d" contains an unsupported document divider';
44 12
        $message  = sprintf($format, $value);
45
46 12
        self::oneOf($value, $haystack, $message);
47 6
    }
48
49
    /**
50
     * Return document dividers array
51
     *
52
     * @return array
53
     */
54 12
    private static function getDocumentDividers(): array
55
    {
56 12
        $constants = [];
57
58
        try {
59 12
            $reportingCloud  = new ReportingCloud();
60 12
            $reflectionClass = new ReflectionClass($reportingCloud);
61 12
            $constants       = $reflectionClass->getConstants();
62 12
            unset($reportingCloud);
63
        } catch (ReflectionException $e) {
64
            // continue
65
        }
66
67
        $ret = array_filter($constants, function (string $constantKey): bool {
68 12
            if (StringUtils::startsWith($constantKey, 'DOCUMENT_DIVIDER_')) {
69 12
                return true;
70
            }
71 12
            return false;
72 12
        }, ARRAY_FILTER_USE_KEY);
73
74 12
        $ret = array_values($ret);
75
76 12
        return $ret;
77
    }
78
}
79