Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Types
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
7 / 7
9
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
1
 apieContext
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 createSingleton
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createMeta
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 fromId
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 displayMeta
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 json
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 null
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Graphql;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\ContextConstants;
6use Apie\Core\Metadata\MetadataFactory;
7use Apie\Graphql\Types\FromMetadataInputType;
8use Apie\Graphql\Types\FromMetadataType;
9use GraphQL\Type\Definition\NonNull;
10use GraphQL\Type\Definition\Type;
11use MLL\GraphQLScalars\MixedScalar;
12use MLL\GraphQLScalars\NullScalar;
13
14final class Types
15{
16    private static ?MixedScalar $json = null;
17    private static ?NullScalar $null = null;
18
19    private static array $createMeta = [];
20
21    private static array $resultMeta = [];
22
23    private static array $created = [];
24
25    /**
26     * @codeCoverageIgnore
27     */
28    private function __construct()
29    {
30    }
31
32    private static function apieContext(): ApieContext
33    {
34        return new ApieContext([
35            ContextConstants::GRAPHQL => 1,
36        ]);
37    }
38
39    public static function createSingleton(string $typeName, callable $factory): Type
40    {
41        return self::$created[$typeName] ??= $factory(self::apieContext());
42    }
43
44    public static function createMeta(\ReflectionClass $class): FromMetadataInputType
45    {
46        return self::$createMeta[$class->name] ??= new FromMetadataInputType(
47            MetadataFactory::getCreationMetadata($class, self::apieContext()),
48            '_create'
49        );
50    }
51
52    public static function fromId(\ReflectionClass $class): Type
53    {
54        $meta = MetadataFactory::getResultMetadata($class, self::apieContext());
55        $type = FromMetadataType::createFromField($meta->getHashmap()['id']);
56        if ($type instanceof NonNull) {
57            return $type->getWrappedType();
58        }
59        return $type;
60    }
61
62    public static function displayMeta(\ReflectionClass $class): FromMetadataType
63    {
64        return self::$resultMeta[$class->name] ??= new FromMetadataType(
65            MetadataFactory::getResultMetadata($class, self::apieContext())
66        );
67    }
68
69    public static function json(): MixedScalar
70    {
71        return self::$json ??= new MixedScalar();
72    }
73
74    public static function null(): NullScalar
75    {
76        return self::$null ??= new NullScalar();
77    }
78
79}