Passed
Branch qa (cee064)
by Jonathan
14:50
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
    /**
32
     * @param mixed $value
33
     *
34
     * @return string
35
     */
36
    abstract protected static function valueToString($value): string;
37
38
    /**
39
     * Check value is a valid DateTime string
40
     *
41
     * @param string $value
42
     * @param string $message
43
     *
44
     * @return void
45
     * @throws InvalidArgumentException
46
     */
47 16
    public static function assertDateTime(string $value, string $message = ''): void
48
    {
49 16
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
50 16
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
51
52 16
        if (self::getDateTimeLength() !== strlen($value)) {
53 2
            $format  = 0 === strlen($message) ? '%1$s has an invalid number of characters in it' : $message;
54 2
            $message = sprintf($format, self::valueToString($value));
55 2
            throw new InvalidArgumentException($message);
56
        }
57
58 14
        $dateTimeZone = new DateTimeZone($timeZone);
59
60
        try {
61 14
            $dateTime = DateTime::createFromFormat($dateFormat, $value, $dateTimeZone);
62 14
            if ($dateTime instanceof DateTime) {
0 ignored issues
show
introduced by
$dateTime is always a sub-type of DateTime.
Loading history...
63 10
                if (0 !== $dateTime->getOffset()) {
64 2
                    $format  = 0 === strlen($message) ? '%1$s has an invalid offset' : $message;
65 2
                    $message = sprintf($format, self::valueToString($value));
66 10
                    throw new InvalidArgumentException($message);
67
                }
68
            } else {
69 4
                $format  = 0 === strlen($message) ? '%1$s is syntactically invalid' : $message;
70 4
                $message = sprintf($format, self::valueToString($value));
71 12
                throw new InvalidArgumentException($message);
72
            }
73 6
        } catch (Exception $e) {
74 6
            $format  = 0 === strlen($message) ? 'Internal error validating %1$s - %2$s' : $message;
75 6
            $message = sprintf($format, self::valueToString($value), self::valueToString($e->getMessage()));
76 6
            throw new InvalidArgumentException($message);
77
        }
78 8
    }
79
80
    /**
81
     * Get the length of the required dateTime string
82
     *
83
     * @return int
84
     */
85 16
    private static function getDateTimeLength()
86
    {
87 16
        $ret = 0;
88
89 16
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
90 16
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
91
92 16
        $dateTimeZone = new DateTimeZone($timeZone);
93
94
        try {
95 16
            $dateTime = new DateTime('now', $dateTimeZone);
96 16
            $ret      = strlen($dateTime->format($dateFormat));
97 16
            unset($dateTime);
98 16
            unset($dateTimeZone);
99
        } catch (Exception $e) {
100
            // continue;
101
        }
102
103 16
        return $ret;
104
    }
105
}
106