Passed
Push — master ( e7bef7...3228c5 )
by Vladimir
03:06
created

ImageType   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 31
dl 0
loc 51
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveUrl() 0 10 2
A __construct() 0 37 1
1
<?php
2
namespace GraphQL\Examples\Blog\Type;
3
4
use GraphQL\Examples\Blog\AppContext;
5
use GraphQL\Examples\Blog\Data\Image;
6
use GraphQL\Examples\Blog\Types;
7
use GraphQL\Type\Definition\EnumType;
8
use GraphQL\Type\Definition\ObjectType;
9
10
class ImageType extends ObjectType
11
{
12
    public function __construct()
13
    {
14
        $config = [
15
            'name' => 'ImageType',
16
            'fields' => [
17
                'id' => Types::id(),
18
                'type' => new EnumType([
19
                    'name' => 'ImageTypeEnum',
20
                    'values' => [
21
                        'USERPIC' => Image::TYPE_USERPIC
22
                    ]
23
                ]),
24
                'size' => Types::imageSizeEnum(),
25
                'width' => Types::int(),
26
                'height' => Types::int(),
27
                'url' => [
28
                    'type' => Types::url(),
29
                    'resolve' => [$this, 'resolveUrl']
30
                ],
31
32
                // Just for the sake of example
33
                'fieldWithError' => [
34
                    'type' => Types::string(),
35
                    'resolve' => function() {
36
                        throw new \Exception("Field with exception");
37
                    }
38
                ],
39
                'nonNullFieldWithError' => [
40
                    'type' => Types::nonNull(Types::string()),
41
                    'resolve' => function() {
42
                        throw new \Exception("Non-null field with exception");
43
                    }
44
                ]
45
            ]
46
        ];
47
48
        parent::__construct($config);
49
    }
50
51
    public function resolveUrl(Image $value, $args, AppContext $context)
52
    {
53
        switch ($value->type) {
54
            case Image::TYPE_USERPIC:
55
                $path = "/images/user/{$value->id}-{$value->size}.jpg";
56
                break;
57
            default:
58
                throw new \UnexpectedValueException("Unexpected image type: " . $value->type);
59
        }
60
        return $context->rootUrl . $path;
61
    }
62
}
63