Passed
Push — master ( 71c3b0...2166ef )
by Jonathan
13:15
created

AssertDateTimeTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 0
Metric Value
wmc 11
eloc 36
dl 0
loc 73
ccs 34
cts 35
cp 0.9714
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B assertDateTime() 0 36 9
A getDateTimeLength() 0 19 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 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
        if (self::getDateTimeLength() !== strlen($value)) {
45 3
            $format  = $message ?: '%s has an invalid number of characters in it';
46 3
            $message = sprintf($format, self::valueToString($value));
47 3
            self::reportInvalidArgument($message);
48
        }
49
50 18
        $dateTimeZone = new DateTimeZone($timeZone);
51
52
        try {
53 18
            $dateTime = DateTime::createFromFormat($dateFormat, $value, $dateTimeZone);
54 18
            if (!$dateTime) {
55 6
                $format  = $message ?: '%s is syntactically invalid';
56 6
                $message = sprintf($format, self::valueToString($value));
57 6
                self::reportInvalidArgument($message);
58
            }
59 12
            if (0 !== $dateTime->getOffset()) {
60 3
                $format  = $message ?: '%s has an invalid offset';
61 3
                $message = sprintf($format, self::valueToString($value));
62 12
                self::reportInvalidArgument($message);
63
            }
64 9
        } catch (Exception $e) {
65 9
            $format  = $message ?: 'Internal error validating %s - %s';
66 9
            $message = sprintf(
67 9
                $format,
68 9
                self::valueToString($value),
69 9
                self::valueToString($e->getMessage())
70
            );
71 9
            self::reportInvalidArgument($message);
72
        }
73
74 9
        return null;
75
    }
76
77
    /**
78
     * Get the length of the required dateTime string
79
     *
80
     * @return int
81
     */
82 21
    private static function getDateTimeLength()
83
    {
84 21
        $ret = 0;
85
86 21
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
87 21
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
88
89 21
        $dateTimeZone = new DateTimeZone($timeZone);
90
91
        try {
92 21
            $dateTime = new DateTime('now', $dateTimeZone);
93 21
            $ret      = strlen($dateTime->format($dateFormat));
94
        } catch (Exception $e) {
95
            // continue;
96
        }
97
98 21
        unset($dateTimeZone, $dateTime);
99
100 21
        return $ret;
101
    }
102
}
103