Passed
Push — master ( fbc447...748ca4 )
by Jonathan
18:21
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
 * @author  Jonathan Maron (@JonathanMaron)
27
 */
28
trait AssertDateTimeTrait
29
{
30
    /**
31
     * Validate DateTime string
32
     *
33
     * @param string $value
34
     * @param string $message
35
     *
36
     * @return null
37
     * @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException
38
     */
39 21
    public static function assertDateTime(string $value, string $message = '')
40
    {
41 21
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
42 21
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
43
44 21
        $dateTimeZone = new DateTimeZone($timeZone);
45
46 21
        if (self::getDateTimeLength() !== strlen($value)) {
47 3
            $format  = '%s has an invalid number of characters in it';
48 3
            $message = sprintf($message ?: $format, self::valueToString($value));
49 3
            self::reportInvalidArgument($message);
50
        }
51
52
        try {
53 18
            $dateTime = DateTime::createFromFormat($dateFormat, $value, $dateTimeZone);
54 18
            if (!$dateTime) {
55 6
                $format  = '%s is syntactically invalid';
56 6
                $message = sprintf($message ?: $format, self::valueToString($value));
57 6
                self::reportInvalidArgument($message);
58
            }
59 12
            if (0 !== $dateTime->getOffset()) {
60 3
                $format  = '%s has an invalid offset';
61 3
                $message = sprintf($message ?: $format, self::valueToString($value));
62 12
                self::reportInvalidArgument($message);
63
            }
64 9
        } catch (Exception $e) {
65 9
            $format  = 'Internal error validating %s - %s';
66 9
            $message = sprintf(
67
                $message ?:
68 9
                $format,
69 9
                self::valueToString($value),
70 9
                self::valueToString($e->getMessage())
71
            );
72 9
            self::reportInvalidArgument($message);
73
        }
74
75 9
        return null;
76
    }
77
78
    /**
79
     * Get the length of the required dateTime string
80
     *
81
     * @return int
82
     */
83 21
    private static function getDateTimeLength()
84
    {
85 21
        $ret = 0;
86
87 21
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
88 21
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
89
90 21
        $dateTimeZone = new DateTimeZone($timeZone);
91
92
        try {
93 21
            $dateTime = new DateTime('now', $dateTimeZone);
94 21
            $ret      = strlen($dateTime->format($dateFormat));
95
        } catch (Exception $e) {
96
            // continue;
97
        }
98
99 21
        unset($dateTimeZone, $dateTime);
100
101 21
        return $ret;
102
    }
103
}
104