Passed
Push — master ( 336684...81d276 )
by Kévin
19:58 queued 14:43
created

UploadType::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\GraphQl\Type\Definition;
15
16
use GraphQL\Error\Error;
17
use GraphQL\Type\Definition\ScalarType;
18
use GraphQL\Utils\Utils;
19
use Symfony\Component\HttpFoundation\File\UploadedFile;
20
21
/**
22
 * Represents an upload type.
23
 *
24
 * @author Mahmood Bazdar <[email protected]>
25
 */
26
final class UploadType extends ScalarType implements TypeInterface
27
{
28
    public function __construct()
29
    {
30
        $this->name = 'Upload';
31
        $this->description = 'The `Upload` type represents a file to be uploaded in the same HTTP request as specified by [graphql-multipart-request-spec](https://github.com/jaydenseric/graphql-multipart-request-spec).';
32
33
        parent::__construct();
34
    }
35
36
    public function getName(): string
37
    {
38
        return $this->name;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function serialize($value)
45
    {
46
        throw new Error('`Upload` cannot be serialized.');
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function parseValue($value): UploadedFile
53
    {
54
        if (!$value instanceof UploadedFile) {
55
            throw new Error(sprintf('Could not get uploaded file, be sure to conform to GraphQL multipart request specification. Instead got: %s', Utils::printSafe($value)));
56
        }
57
58
        return $value;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function parseLiteral($valueNode, array $variables = null)
65
    {
66
        throw new Error('`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification.', $valueNode);
67
    }
68
}
69