Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.95% covered (warning)
80.95%
17 / 21
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
FromMetadataType
80.95% covered (warning)
80.95%
17 / 21
0.00% covered (danger)
0.00%
0 / 1
6.25
0.00% covered (danger)
0.00%
0 / 1
 __construct
80.95% covered (warning)
80.95%
17 / 21
0.00% covered (danger)
0.00%
0 / 1
6.25
1<?php
2namespace Apie\Graphql\Types;
3
4use Apie\Core\Attributes\Description;
5use Apie\Core\Metadata\MetadataInterface;
6use Apie\Core\ValueObjects\Utils;
7use Apie\Graphql\Concerns\CreatesFromMeta;
8use GraphQL\Type\Definition\ObjectType;
9use GraphQL\Type\Definition\Type;
10use ReflectionAttribute;
11
12class FromMetadataType extends ObjectType
13{
14    use CreatesFromMeta;
15
16    public function __construct(MetadataInterface $metadata, string $suffix = '')
17    {
18        $class = $metadata->toClass();
19        $name = $class ? Utils::getDisplayNameForValueObject($class) : $metadata->toScalarType()->value;
20        $config = [
21            'name' => $name . $suffix,
22            'fields' => [
23            ],
24        ];
25
26        foreach ($metadata->toClass()?->getAttributes(Description::class, ReflectionAttribute::IS_INSTANCEOF) ?? [] as $descriptionAttribute) {
27            $description = $descriptionAttribute->newInstance();
28            $config['description'] = $description->description;
29        }
30
31        foreach ($metadata->getHashmap() as $name => $field) {
32            if ($field->isField()) {
33                $config['fields'][$name] = [
34                    'type' => self::createFromField($field),
35                ];
36            }
37        }
38        if (empty($config['fields'])) {
39            $config['fields']['_'] = [
40                'type' => Type::string(),
41                'description' => 'This is a dummy field, because GraphQL does not allow empty input types.',
42            ];
43        }
44        parent::__construct($config);
45    }
46}