Date::parseValue()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 24
ccs 15
cts 15
cp 1
crap 6
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\Type;
6
7
use GraphQL\Error\Error;
8
use GraphQL\Error\InvariantViolation;
9
use GraphQL\Language\AST\Node;
10
use GraphQL\Language\AST\StringValueNode;
11
use GraphQL\Language\Printer;
12
use GraphQL\Utils\Utils;
13
14
final class Date extends AbstractScalarType
15
{
16
    protected string $name = 'Date';
17
18
    protected string $description = <<<'STR'
19
        The `Date` scalar type represents date data,
20
        represented as an ISO-8601 encoded UTC date string.';
21
        STR;
22
23 3
    public function serialize(mixed $value): string
24
    {
25 3
        if (! $value instanceof \DateTimeInterface) {
26 1
            throw new InvariantViolation(
27 1
                'Date is not an instance of DateTimeInterface: ' . Utils::printSafe($value)
28 1
            );
29
        }
30
31 2
        return $value->format('Y-m-d');
32
    }
33
34 5
    public function parseValue(mixed $value): ?\DateTimeImmutable
35
    {
36 5
        if (null === $value) {
37 1
            return null;
38
        }
39
40 4
        if ($value instanceof \DateTimeImmutable) {
41 1
            return $value;
42
        }
43
44 3
        $str = (string) $value;
45
46 3
        $dateTime = \DateTimeImmutable::createFromFormat('!Y-m-d', $str, new \DateTimeZone('UTC'));
47
48 3
        $errors = \DateTimeImmutable::getLastErrors() ?: ['error_count' => 0, 'warnings' => []];
49
50 3
        if ($errors['error_count'] > 0 || \count($errors['warnings'])) {
51 2
            throw new Error(\sprintf(
52 2
                'The Date value must be a string value in "Y-m-d" format; given: %s',
53 2
                Utils::printSafeJson($value),
54 2
            ));
55
        }
56 1
        \assert($dateTime instanceof \DateTimeImmutable);
57 1
        return $dateTime;
58
    }
59
60 4
    public function parseLiteral(Node $valueNode, ?array $variables = null): \DateTimeImmutable
61
    {
62 4
        if ($valueNode instanceof StringValueNode) {
63 3
            $dateTime = \DateTimeImmutable::createFromFormat('!Y-m-d', $valueNode->value, new \DateTimeZone('UTC'));
64
65 3
            $errors = \DateTimeImmutable::getLastErrors() ?: ['error_count' => 0, 'warnings' => []];
66
67 3
            if ($errors['error_count'] > 0 || \count($errors['warnings'])) {
68 2
                throw new Error(
69 2
                    \sprintf('Invalid Date value; given: %s', Printer::doPrint($valueNode)),
70 2
                    $valueNode,
0 ignored issues
show
Bug introduced by
$valueNode of type GraphQL\Language\AST\StringValueNode is incompatible with the type iterable expected by parameter $nodes of GraphQL\Error\Error::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
                    /** @scrutinizer ignore-type */ $valueNode,
Loading history...
71 2
                );
72
            }
73 1
            \assert($dateTime instanceof \DateTimeImmutable);
74 1
            return $dateTime;
75
        }
76
77 1
        throw new Error(\sprintf(
78 1
            'The Date value must be a string value in "Y-m-d" format; given: %s',
79 1
            Printer::doPrint($valueNode),
80 1
        ));
81
    }
82
}
83