Completed
Push — master ( f446c9...f7ee24 )
by Rafael
04:05
created

UploadType   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 45
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parseValue() 0 10 2
A serialize() 0 3 1
A parseLiteral() 0 5 1
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Type;
12
13
use GraphQL\Error\Error;
14
use GraphQL\Error\InvariantViolation;
15
use GraphQL\Type\Definition\ScalarType;
16
use GraphQL\Utils\Utils;
17
use Symfony\Component\HttpFoundation\File\UploadedFile;
18
19
/**
20
 * The `Upload` special type represents a file to be uploaded in the same HTTP request as specified by
21
 * @see https://github.com/jaydenseric/graphql-multipart-request-spec
22
 */
23
class UploadType extends ScalarType
24
{
25
    /**
26
     * @var string
27
     */
28
    public $name = 'Upload';
29
30
    /**
31
     * @var string
32
     */
33
    public $description =
34
        'The `Upload` special type represents a file to be uploaded in the same HTTP request as specified by
35
 [graphql-multipart-request-spec](https://github.com/jaydenseric/graphql-multipart-request-spec).';
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function serialize($value)
41
    {
42
        throw new InvariantViolation('`Upload` cannot be serialized');
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function parseValue($value)
49
    {
50
        if (!$value instanceof UploadedFile) {
51
            throw new \UnexpectedValueException(
52
                'Could not get uploaded file, be sure to conform to GraphQL multipart request specification. 
53
Instead got: '.Utils::printSafe($value)
54
            );
55
        }
56
57
        return $value;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function parseLiteral($valueNode)
64
    {
65
        throw new Error(
66
            '`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification.
67
Instead got: '.$valueNode->kind, [$valueNode]
68
        );
69
    }
70
}
71