Passed
Branch development-2.0 (5c2c30)
by Jonathan
04:58
created

AssertDateTimeTrait::getDateTimeLength()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
ccs 9
cts 10
cp 0.9
rs 9.9332
c 0
b 0
f 0
cc 2
nc 3
nop 0
crap 2.004
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 DateTime;
18
use DateTimeZone;
19
use Exception;
20
use TxTextControl\ReportingCloud\ReportingCloud;
21
22
/**
23
 * Trait AssertDateTimeTrait
24
 *
25
 * @package TxTextControl\ReportingCloud
26
 */
27
trait AssertDateTimeTrait
28
{
29
    /**
30
     * Validate DateTime string
31
     *
32
     * @param string $value
33
     * @param string $message
34
     *
35
     * @return null
36
     * @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException
37
     */
38 7
    public static function assertDateTime(string $value, string $message = '')
39
    {
40 7
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
41 7
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
42
43 7
        $dateTimeZone = new DateTimeZone($timeZone);
44
45 7
        if (self::getDateTimeLength() !== strlen($value)) {
46 1
            $format  = '%s has an invalid number of characters in it';
47 1
            $message = sprintf($message ?: $format, self::valueToString($value));
48 1
            self::reportInvalidArgument($message);
49
        }
50
51
        try {
52 6
            $dateTime = DateTime::createFromFormat($dateFormat, $value, $dateTimeZone);
53 6
            if (!$dateTime) {
54 2
                $format  = '%s is syntactically invalid';
55 2
                $message = sprintf($message ?: $format, self::valueToString($value));
56 2
                self::reportInvalidArgument($message);
57
            }
58 4
            if (0 !== $dateTime->getOffset()) {
59 1
                $format  = '%s has an invalid offset';
60 1
                $message = sprintf($message ?: $format, self::valueToString($value));
61 4
                self::reportInvalidArgument($message);
62
            }
63 3
        } catch (Exception $e) {
64 3
            $format  = 'Internal error validating %s - %s';
65 3
            $message = sprintf($message ?: $format,
66 3
                self::valueToString($value),
67 3
                self::valueToString($e->getMessage())
68
            );
69 3
            self::reportInvalidArgument($message);
70
        }
71
72 3
        return null;
73
    }
74
75
    /**
76
     * Get the length of the required dateTime string
77
     *
78
     * @return int
79
     */
80 7
    private static function getDateTimeLength()
81
    {
82 7
        $ret = 0;
83
84 7
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
85 7
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
86
87 7
        $dateTimeZone = new DateTimeZone($timeZone);
88
89
        try {
90 7
            $dateTime = new DateTime('now', $dateTimeZone);
91 7
            $ret      = strlen($dateTime->format($dateFormat));
92
        } catch (Exception $e) {
93
            // continue;
94
        }
95
96 7
        unset($dateTimeZone, $dateTime);
97
98 7
        return $ret;
99
    }
100
}
101