Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.77% |
60 / 62 |
|
81.82% |
9 / 11 |
CRAP | |
0.00% |
0 / 1 |
| Types | |
96.77% |
60 / 62 |
|
81.82% |
9 / 11 |
22 | |
0.00% |
0 / 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% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| createMeta | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| methodCallMeta | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
2.01 | |||
| modifyMeta | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| fromId | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| displayMeta | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| 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 | if (isset(self::$created[$typeName])) { |
| 78 | return self::$created[$typeName]; |
| 79 | } |
| 80 | self::$created[$typeName] = self::json(); |
| 81 | return self::$created[$typeName] = $factory(self::apieContext()); |
| 82 | } |
| 83 | |
| 84 | public static function createMeta(\ReflectionClass $class): FromMetadataInputType |
| 85 | { |
| 86 | if (isset(self::$createMeta[$class->name])) { |
| 87 | return self::$createMeta[$class->name]; |
| 88 | } |
| 89 | self::$createMeta[$class->name] = self::json(); |
| 90 | return self::$createMeta[$class->name] = new FromMetadataInputType( |
| 91 | MetadataFactory::getCreationMetadata($class, self::apieContext()), |
| 92 | '_create' |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | public static function methodCallMeta(\ReflectionMethod $method): FromMetadataInputType |
| 97 | { |
| 98 | $key = $method->getDeclaringClass()->name . '::' . $method->name; |
| 99 | if (isset(self::$methodCallMeta[$key])) { |
| 100 | return self::$methodCallMeta[$key]; |
| 101 | } |
| 102 | self::$methodCallMeta[$key] = self::json(); |
| 103 | return self::$methodCallMeta[$key] = new FromMetadataInputType( |
| 104 | MetadataFactory::getMethodMetadata($method, self::apieContext()), |
| 105 | 'Run' . ucfirst($method->name) |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | public static function modifyMeta(\ReflectionClass $class): FromMetadataInputType |
| 111 | { |
| 112 | if (isset(self::$modifyMeta[$class->name])) { |
| 113 | return self::$modifyMeta[$class->name]; |
| 114 | } |
| 115 | self::$modifyMeta[$class->name] = self::json(); |
| 116 | return self::$modifyMeta[$class->name] = new FromMetadataInputType( |
| 117 | MetadataFactory::getModificationMetadata($class, self::apieContext()), |
| 118 | '_modify' |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | public static function fromId(\ReflectionClass $class): Type |
| 123 | { |
| 124 | $meta = MetadataFactory::getResultMetadata($class, self::apieContext()); |
| 125 | $type = FromMetadataType::createFromField($meta->getHashmap()['id']); |
| 126 | if ($type instanceof NonNull) { |
| 127 | return $type->getWrappedType(); |
| 128 | } |
| 129 | return $type; |
| 130 | } |
| 131 | |
| 132 | public static function displayMeta(\ReflectionClass $class): FromMetadataType |
| 133 | { |
| 134 | if (isset(self::$resultMeta[$class->name])) { |
| 135 | return self::$resultMeta[$class->name]; |
| 136 | } |
| 137 | |
| 138 | self::$resultMeta[$class->name] = self::json(); |
| 139 | |
| 140 | return self::$resultMeta[$class->name] = new FromMetadataType( |
| 141 | MetadataFactory::getResultMetadata($class, self::apieContext()) |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | public static function json(): MixedScalar |
| 146 | { |
| 147 | return self::$json ??= new MixedScalar(); |
| 148 | } |
| 149 | |
| 150 | public static function null(): NullScalar |
| 151 | { |
| 152 | return self::$null ??= new NullScalar(); |
| 153 | } |
| 154 | |
| 155 | } |