UploadType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 39
ccs 11
cts 11
cp 1
rs 10
c 3
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 3 1
A parseLiteral() 0 3 1
A parseValue() 0 12 3
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 11
    public function parseValue(mixed $value): UploadedFileInterface
35
    {
36 11
        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 10
        $error = $value->getError();
41 10
        if ($error !== UPLOAD_ERR_OK) {
42 8
            throw new UploadError($error);
43
        }
44
45 2
        return $value;
46
    }
47
48
    /**
49
     * Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input.
50
     */
51 1
    public function parseLiteral(Node $valueNode, ?array $variables = null): mixed
52
    {
53 1
        throw new Error('`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification. Instead got: ' . $valueNode->kind, $valueNode);
54
    }
55
}
56