Completed
Push — master ( 5ac91e...39483e )
by Kirill
38:30
created

DateTimeType   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 51
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isCompatible() 0 8 2
A verifyDate() 0 9 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Standard\Scalars;
11
12
use Railt\SDL\Contracts\Document;
13
use Railt\SDL\Standard\StandardType;
14
15
/**
16
 * RFC315 Implementation.
17
 *
18
 * @see https://github.com/facebook/graphql/pull/315
19
 * @see https://github.com/graphql/graphql-js/issues/550
20
 * @see https://github.com/graphql/graphql-js/pull/557
21
 */
22
final class DateTimeType extends StringType implements StandardType
23
{
24
    /**
25
     * The DateTime scalar public name constant.
26
     * This name will be used in the future as the
27
     * type name available for use in our schema.
28
     */
29
    protected const SCALAR_TYPE_NAME = 'DateTime';
30
31
    /**
32
     * Short DateTime scalar public description.
33
     */
34
    protected const TYPE_DESCRIPTION = 'The complete set of date and time formats specified in ISO8601 
35
        is quite complex in an attempt to provide multiple representations and partial representations.';
36
37
    /**
38
     * @param Document $document
39
     */
40 283
    public function __construct(Document $document)
41
    {
42 283
        parent::__construct($document);
43 283
        $this->deprecationReason = static::RFC_IMPL_DESCRIPTION;
44 283
    }
45
46
    /**
47
     * @param mixed|string $value
48
     * @return bool
49
     */
50 420
    public function isCompatible($value): bool
51
    {
52 420
        if (\is_string($value)) {
53 300
            return $this->verifyDate($value);
54
        }
55
56 120
        return false;
57
    }
58
59
    /**
60
     * @param $value
61
     * @return bool
62
     */
63 300
    private function verifyDate($value): bool
64
    {
65
        try {
66 300
            new \DateTime($value);
67 300
            return true;
68
        } catch (\Throwable $error) {
69
            return false;
70
        }
71
    }
72
}
73