UploadType::parseValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
ccs 4
cts 4
cp 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Upload;
6
7
use GraphQL\Error\Error;
8
use GraphQL\Error\InvariantViolation;
9
use GraphQL\Language\AST\Node;
10
use GraphQL\Type\Definition\ScalarType;
11
use GraphQL\Utils\Utils;
12
use Psr\Http\Message\UploadedFileInterface;
13
use UnexpectedValueException;
14
15
final class UploadType extends ScalarType
16
{
17
    public string $name = 'Upload';
18
19
    public ?string $description
20
        = 'The `Upload` special type represents a file to be uploaded in the same HTTP request as specified by
21
 [graphql-multipart-request-spec](https://github.com/jaydenseric/graphql-multipart-request-spec).';
22
23
    /**
24
     * Serializes an internal value to include in a response.
25
     */
26 1
    public function serialize(mixed $value): never
27
    {
28 1
        throw new InvariantViolation('`Upload` cannot be serialized');
29
    }
30
31
    /**
32
     * Parses an externally provided value (query variable) to use as an input.
33
     */
34 3
    public function parseValue(mixed $value): UploadedFileInterface
35
    {
36 3
        if (!$value instanceof UploadedFileInterface) {
37 1
            throw new UnexpectedValueException('Could not get uploaded file, be sure to conform to GraphQL multipart request specification. Instead got: ' . Utils::printSafe($value));
38
        }
39
40 2
        return $value;
41
    }
42
43
    /**
44
     * Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input.
45
     */
46 1
    public function parseLiteral(Node $valueNode, ?array $variables = null): mixed
47
    {
48 1
        throw new Error('`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification. Instead got: ' . $valueNode->kind, $valueNode);
49
    }
50
}
51