Passed
Branch development-2.0 (469a91)
by Jonathan
04:44
created

AssertDateTimeTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 11
eloc 37
dl 0
loc 74
ccs 34
cts 36
cp 0.9444
rs 10
c 0
b 0
f 0

2 Methods

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