SchemaWarmupper   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 85.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 60
c 1
b 0
f 0
dl 0
loc 107
ccs 53
cts 62
cp 0.8548
rs 10
wmc 25

2 Methods

Rating   Name   Duplication   Size   Complexity  
B warmup() 0 32 8
C warmupType() 0 71 17
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\Spiral\Common;
6
7
use GraphQL\Type\Definition as Webonyx;
8
use GraphQL\Type\Schema;
9
10
final class SchemaWarmupper
11
{
12 7
    public static function warmup(Schema $schema): void
13
    {
14 7
        $allTypes = [];
15
16 7
        $types = $schema->getConfig()->getTypes();
17 7
        if (\is_callable($types)) {
18 7
            $types = $types();
19
        }
20
21 7
        foreach ($types as $type) {
22 7
            self::warmupType(Schema::resolveType($type), $allTypes);
23
        }
24
25 7
        foreach ($schema->getDirectives() as $directive) {
26 7
            foreach ($directive->args as $arg) {
27 7
                self::warmupType($arg->getType(), $allTypes);
28
            }
29
        }
30
31 7
        if ($type = $schema->getQueryType()) {
32 7
            self::warmupType($type, $allTypes);
33
        }
34
35 7
        if ($type = $schema->getMutationType()) {
36 7
            self::warmupType($type, $allTypes);
37
        }
38
39 7
        if ($type = $schema->getSubscriptionType()) {
40
            self::warmupType($type, $allTypes);
41
        }
42
43 7
        $schema->getTypeMap();
44
    }
45
46 7
    private static function warmupType(Webonyx\Type $type, array &$allTypes): void
47
    {
48 7
        if ($type instanceof Webonyx\WrappingType) {
49 7
            self::warmupType($type->getInnermostType(), $allTypes);
50
51 7
            return;
52
        }
53 7
        \assert($type instanceof Webonyx\NamedType);
54
        /**
55
         * @psalm-suppress NoInterfaceProperties
56
         * @psalm-suppress UndefinedPropertyFetch
57
         */
58 7
        $name = $type->name;
59 7
        if (isset($allTypes[$name])) {
60 7
            return;
61
        }
62
63 7
        $allTypes[$name] = true;
64
65 7
        if ($type instanceof Webonyx\EnumType) {
66 7
            $enumValues = $type->getValues();
67 7
            if (\is_callable($type->config['values'])) {
68
                $values = [];
69
                foreach ($enumValues as $value) {
70
                    $values[$value->name] = [
71
                        'value' => $value->value,
72
                        'description' => $value->description,
73
                        'deprecationReason' => $value->deprecationReason,
74
                    ];
75
                }
76
                $type->config['values'] = $values;
77
            }
78 7
            return;
79
        }
80
81 7
        if ($type instanceof Webonyx\UnionType) {
82 7
            $type->config['types'] = $type->getTypes();
83 7
            foreach ($type->config['types'] as $member) {
84 7
                self::warmupType($member, $allTypes);
85
            }
86 7
            return;
87
        }
88
89 7
        if ($type instanceof Webonyx\InputObjectType) {
90 7
            $type->config['fields'] = $type->getFields();
91 7
            foreach ($type->config['fields'] as $field) {
92 7
                self::warmupType($field->getType(), $allTypes);
93
            }
94 7
            return;
95
        }
96
97 7
        if ($type instanceof Webonyx\ImplementingType) {
98 7
            $interfaces = $type->getInterfaces();
99 7
            foreach ($interfaces as $interface) {
100 7
                self::warmupType($interface, $allTypes);
101
            }
102 7
            \assert($type instanceof Webonyx\ObjectType || $type instanceof Webonyx\InterfaceType);
103 7
            $type->config['interfaces'] = $interfaces;
104
        }
105
106 7
        if ($type instanceof Webonyx\HasFieldsType) {
107 7
            $fields = $type->getFields();
108 7
            foreach ($fields as $field) {
109 7
                foreach ($field->args as $arg) {
110 7
                    self::warmupType($arg->config['type'] = $arg->getType(), $allTypes);
111
                }
112 7
                self::warmupType($field->getType(), $allTypes);
113
            }
114
115 7
            \assert($type instanceof Webonyx\ObjectType || $type instanceof Webonyx\InterfaceType);
116 7
            $type->config['fields'] = $fields;
117
        }
118
    }
119
}
120