Passed
Branch development-2.0 (f1893c)
by Jonathan
06:14
created

AssertDateTimeTrait::assertDateTime()   B

Complexity

Conditions 11
Paths 78

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 11

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 39
ccs 27
cts 27
cp 1
rs 7.3166
c 0
b 0
f 0
cc 11
nc 78
nop 2
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Throwable;
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
     */
37 52
    public static function assertDateTime(string $value, string $message = '')
38
    {
39 52
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
40 52
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
41
42 52
        $dateTimeZone = new DateTimeZone($timeZone);
43
44
        try {
45 52
            $dateTime = new DateTime('now', $dateTimeZone);
46 52
            if (strlen($dateTime->format($dateFormat)) !== strlen($value)) {
47 1
                $format  = '%s has an invalid number of characters in it';
48 1
                $message = sprintf($message ?: $format, static::valueToString($value));
49 52
                static::reportInvalidArgument($message);
50
            }
51 1
        } catch (Throwable $e) {
52 1
            $format  = 'Internal error validating %s';
53 1
            $message = sprintf($message ?: $format, static::valueToString($value));
54 1
            static::reportInvalidArgument($message);
55
        }
56
57
        try {
58 51
            $dateTime = DateTime::createFromFormat($dateFormat, $value, $dateTimeZone);
59 51
            if (!$dateTime) {
60 2
                $format  = '%s is syntactically invalid';
61 2
                $message = sprintf($message ?: $format, static::valueToString($value));
62 2
                static::reportInvalidArgument($message);
63
            }
64 49
            if (0 !== $dateTime->getOffset()) {
65 1
                $format  = '%s has an invalid offset';
66 1
                $message = sprintf($message ?: $format, static::valueToString($value));
67 49
                static::reportInvalidArgument($message);
68
            }
69 3
        } catch (Throwable $e) {
70 3
            $format  = 'Internal error validating %s';
71 3
            $message = sprintf($message ?: $format, static::valueToString($value));
72 3
            static::reportInvalidArgument($message);
73
        }
74
75 48
        return null;
76
    }
77
}
78