Passed
Push — master ( b8e2b8...5d024f )
by Jonathan
17:35
created

AssertDateTimeTrait::getDateTimeLength()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
rs 9.9
cc 2
nc 3
nop 0
crap 2.003
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 © 2021 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Assert;
16
17
use DateTime;
18
use DateTimeZone;
19
use Exception;
20
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
21
use TxTextControl\ReportingCloud\ReportingCloud;
22
23
/**
24
 * Trait AssertDateTimeTrait
25
 *
26
 * @package TxTextControl\ReportingCloud
27
 * @author  Jonathan Maron (@JonathanMaron)
28
 */
29
trait AssertDateTimeTrait
30
{
31
    use ValueToStringTrait;
32
33
    /**
34
     * Check value is a valid DateTime string
35
     *
36
     * @param string $value
37
     * @param string $message
38
     *
39
     * @return void
40
     * @throws InvalidArgumentException
41
     */
42 16
    public static function assertDateTime(string $value, string $message = ''): void
43
    {
44 16
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
45 16
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
46
47 16
        if (self::getDateTimeLength() !== strlen($value)) {
48 2
            $format  = 0 === strlen($message) ? '%1$s has an invalid number of characters in it' : $message;
49 2
            $message = sprintf($format, self::valueToString($value));
50 2
            throw new InvalidArgumentException($message);
51
        }
52
53 14
        $dateTimeZone = new DateTimeZone($timeZone);
54
55
        try {
56 14
            $dateTime = DateTime::createFromFormat($dateFormat, $value, $dateTimeZone);
57 14
            if ($dateTime instanceof DateTime) {
0 ignored issues
show
introduced by
$dateTime is always a sub-type of DateTime.
Loading history...
58 10
                if (0 !== $dateTime->getOffset()) {
59 2
                    $format  = 0 === strlen($message) ? '%1$s has an invalid offset' : $message;
60 2
                    $message = sprintf($format, self::valueToString($value));
61 10
                    throw new InvalidArgumentException($message);
62
                }
63
            } else {
64 4
                $format  = 0 === strlen($message) ? '%1$s is syntactically invalid' : $message;
65 4
                $message = sprintf($format, self::valueToString($value));
66 12
                throw new InvalidArgumentException($message);
67
            }
68 6
        } catch (Exception $e) {
69 6
            $format  = 0 === strlen($message) ? 'Internal error validating %1$s - %2$s' : $message;
70 6
            $message = sprintf($format, self::valueToString($value), self::valueToString($e->getMessage()));
71 6
            throw new InvalidArgumentException($message);
72
        }
73 8
    }
74
75
    /**
76
     * Get the length of the required dateTime string
77
     *
78
     * @return int
79
     */
80 16
    private static function getDateTimeLength(): int
81
    {
82 16
        $ret = 0;
83
84 16
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
85 16
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
86
87 16
        $dateTimeZone = new DateTimeZone($timeZone);
88
89
        try {
90 16
            $dateTime = new DateTime('now', $dateTimeZone);
91 16
            $ret      = strlen($dateTime->format($dateFormat));
92 16
            unset($dateTime);
93 16
            unset($dateTimeZone);
94
        } catch (Exception $e) {
95
            // continue;
96
        }
97
98 16
        return $ret;
99
    }
100
}
101