Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
47 / 47 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| Types | |
100.00% |
47 / 47 |
|
100.00% |
11 / 11 |
17 | |
100.00% |
1 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| clear | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| getDefinedTypes | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
| apieContext | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| createSingleton | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createMeta | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| methodCallMeta | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| modifyMeta | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| fromId | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| displayMeta | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| json | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| null | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Graphql; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\ContextConstants; |
| 6 | use Apie\Core\Metadata\MetadataFactory; |
| 7 | use Apie\Graphql\Types\FromMetadataInputType; |
| 8 | use Apie\Graphql\Types\FromMetadataType; |
| 9 | use GraphQL\Type\Definition\NonNull; |
| 10 | use GraphQL\Type\Definition\Type; |
| 11 | use MLL\GraphQLScalars\MixedScalar; |
| 12 | use MLL\GraphQLScalars\NullScalar; |
| 13 | |
| 14 | final 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 | public static function clear() |
| 37 | { |
| 38 | self::$json = null; |
| 39 | self::$null = null; |
| 40 | self::$createMeta = []; |
| 41 | self::$modifyMeta = []; |
| 42 | self::$methodCallMeta = []; |
| 43 | self::$resultMeta = []; |
| 44 | self::$created = []; |
| 45 | } |
| 46 | |
| 47 | public static function getDefinedTypes(): array |
| 48 | { |
| 49 | $list = [ |
| 50 | ...self::$created, |
| 51 | ]; |
| 52 | foreach (self::$createMeta as $meta) { |
| 53 | $list[$meta->name] = $meta; |
| 54 | } |
| 55 | foreach (self::$modifyMeta as $meta) { |
| 56 | $list[$meta->name] = $meta; |
| 57 | } |
| 58 | foreach (self::$methodCallMeta as $meta) { |
| 59 | $list[$meta->name] = $meta; |
| 60 | } |
| 61 | foreach (self::$resultMeta as $meta) { |
| 62 | $list[$meta->name] = $meta; |
| 63 | } |
| 64 | ksort($list); |
| 65 | return $list; |
| 66 | } |
| 67 | |
| 68 | private static function apieContext(): ApieContext |
| 69 | { |
| 70 | return new ApieContext([ |
| 71 | ContextConstants::GRAPHQL => 1, |
| 72 | ]); |
| 73 | } |
| 74 | |
| 75 | public static function createSingleton(string $typeName, callable $factory): Type |
| 76 | { |
| 77 | return self::$created[$typeName] ??= $factory(self::apieContext()); |
| 78 | } |
| 79 | |
| 80 | public static function createMeta(\ReflectionClass $class): FromMetadataInputType |
| 81 | { |
| 82 | return self::$createMeta[$class->name] ??= new FromMetadataInputType( |
| 83 | MetadataFactory::getCreationMetadata($class, self::apieContext()), |
| 84 | '_create' |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | public static function methodCallMeta(\ReflectionMethod $method): FromMetadataInputType |
| 89 | { |
| 90 | $key = $method->getDeclaringClass()->name . '::' . $method->name; |
| 91 | return self::$methodCallMeta[$key] ??= new FromMetadataInputType( |
| 92 | MetadataFactory::getMethodMetadata($method, self::apieContext()), |
| 93 | 'Run' . ucfirst($method->name) |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | public static function modifyMeta(\ReflectionClass $class): FromMetadataInputType |
| 99 | { |
| 100 | return self::$modifyMeta[$class->name] ??= new FromMetadataInputType( |
| 101 | MetadataFactory::getModificationMetadata($class, self::apieContext()), |
| 102 | '_modify' |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | public static function fromId(\ReflectionClass $class): Type |
| 107 | { |
| 108 | $meta = MetadataFactory::getResultMetadata($class, self::apieContext()); |
| 109 | $type = FromMetadataType::createFromField($meta->getHashmap()['id']); |
| 110 | if ($type instanceof NonNull) { |
| 111 | return $type->getWrappedType(); |
| 112 | } |
| 113 | return $type; |
| 114 | } |
| 115 | |
| 116 | public static function displayMeta(\ReflectionClass $class): FromMetadataType |
| 117 | { |
| 118 | return self::$resultMeta[$class->name] ??= new FromMetadataType( |
| 119 | MetadataFactory::getResultMetadata($class, self::apieContext()) |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | public static function json(): MixedScalar |
| 124 | { |
| 125 | return self::$json ??= new MixedScalar(); |
| 126 | } |
| 127 | |
| 128 | public static function null(): NullScalar |
| 129 | { |
| 130 | return self::$null ??= new NullScalar(); |
| 131 | } |
| 132 | |
| 133 | } |