Failed Conditions
Push — master ( e75200...fab761 )
by Adrien
02:43
created

TimeType::parseLiteral()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Api\Scalar;
6
7
use Cake\Chronos\ChronosTime;
1 ignored issue
show
Bug introduced by
The type Cake\Chronos\ChronosTime was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use GraphQL\Error\Error;
9
use GraphQL\Language\AST\Node;
10
use GraphQL\Language\AST\StringValueNode;
11
use GraphQL\Type\Definition\ScalarType;
12
use GraphQL\Utils\Utils;
13
use UnexpectedValueException;
14
15
final class TimeType extends ScalarType
16
{
17
    public ?string $description = 'A time of the day (local time, no timezone).';
18
19
    /**
20
     * Serializes an internal value to include in a response.
21
     */
22 1
    public function serialize(mixed $value): mixed
23
    {
24 1
        if ($value instanceof ChronosTime) {
25 1
            return $value->format('H:i:s.u');
26
        }
27
28
        return $value;
29
    }
30
31
    /**
32
     * Parses an externally provided value (query variable) to use as an input.
33
     */
34 2
    public function parseValue(mixed $value): ?ChronosTime
35
    {
36 2
        if (!is_string($value)) {
37
            throw new UnexpectedValueException('Cannot represent value as Chronos time: ' . Utils::printSafe($value));
38
        }
39
40 2
        if ($value === '') {
41
            return null;
42
        }
43
44 2
        $time = new ChronosTime($value);
45
46 2
        return $time;
47
    }
48
49
    /**
50
     * Parses an externally provided literal value to use as an input (e.g. in Query AST).
51
     */
52 3
    public function parseLiteral(Node $valueNode, ?array $variables = null): ?ChronosTime
53
    {
54
        // Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL
55
        // error location in query:
56 3
        if (!($valueNode instanceof StringValueNode)) {
57 1
            throw new Error('Query error: Can only parse strings got: ' . $valueNode->kind, $valueNode);
58
        }
59
60 2
        return $this->parseValue($valueNode->value);
61
    }
62
}
63