Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
Types
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
9 / 9
11
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
 methodCallMeta
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 modifyMeta
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 $modifyMeta = [];
22
23    private static array $methodCallMeta = [];
24
25    private static array $resultMeta = [];
26
27    private static array $created = [];
28
29    /**
30     * @codeCoverageIgnore
31     */
32    private function __construct()
33    {
34    }
35
36    private static function apieContext(): ApieContext
37    {
38        return new ApieContext([
39            ContextConstants::GRAPHQL => 1,
40        ]);
41    }
42
43    public static function createSingleton(string $typeName, callable $factory): Type
44    {
45        return self::$created[$typeName] ??= $factory(self::apieContext());
46    }
47
48    public static function createMeta(\ReflectionClass $class): FromMetadataInputType
49    {
50        return self::$createMeta[$class->name] ??= new FromMetadataInputType(
51            MetadataFactory::getCreationMetadata($class, self::apieContext()),
52            '_create'
53        );
54    }
55
56    public static function methodCallMeta(\ReflectionMethod $method): FromMetadataInputType
57    {
58        $key = $method->getDeclaringClass()->name . '::' . $method->name;
59        return self::$methodCallMeta[$key] ??= new FromMetadataInputType(
60            MetadataFactory::getMethodMetadata($method, self::apieContext()),
61            'Run' . ucfirst($method->name)
62        );
63    }
64
65
66    public static function modifyMeta(\ReflectionClass $class): FromMetadataInputType
67    {
68        return self::$modifyMeta[$class->name] ??= new FromMetadataInputType(
69            MetadataFactory::getModificationMetadata($class, self::apieContext()),
70            '_modify'
71        );
72    }
73
74    public static function fromId(\ReflectionClass $class): Type
75    {
76        $meta = MetadataFactory::getResultMetadata($class, self::apieContext());
77        $type = FromMetadataType::createFromField($meta->getHashmap()['id']);
78        if ($type instanceof NonNull) {
79            return $type->getWrappedType();
80        }
81        return $type;
82    }
83
84    public static function displayMeta(\ReflectionClass $class): FromMetadataType
85    {
86        return self::$resultMeta[$class->name] ??= new FromMetadataType(
87            MetadataFactory::getResultMetadata($class, self::apieContext())
88        );
89    }
90
91    public static function json(): MixedScalar
92    {
93        return self::$json ??= new MixedScalar();
94    }
95
96    public static function null(): NullScalar
97    {
98        return self::$null ??= new NullScalar();
99    }
100
101}