Passed
Pull Request — main (#117)
by Tom
02:58
created

TypeManager::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\GraphQL\Type;
6
7
use ApiSkeletons\Doctrine\GraphQL\AbstractContainer;
8
use GraphQL\Error\Error;
9
use GraphQL\Type\Definition\Type;
10
use ReflectionClass;
11
use ReflectionException;
12
13
use function assert;
14
15
class TypeManager extends AbstractContainer
16
{
17
    public function __construct()
18
    {
19
        $this
20
            ->set('tinyint', static fn () => Type::int())
21
            ->set('smallint', static fn () => Type::int())
22
            ->set('integer', static fn () => Type::int())
23
            ->set('int', static fn () => Type::int())
24
            ->set('boolean', static fn () => Type::boolean())
25
            ->set('decimal', static fn () => Type::float())
26
            ->set('float', static fn () => Type::float())
27
            ->set('bigint', static fn () => Type::string())
28
            ->set('string', static fn () => Type::string())
29
            ->set('text', static fn () => Type::string())
30
            ->set('array', static fn () => Type::listOf(Type::string()))
31
            ->set('simple_array', static fn () => Type::listOf(Type::string()))
32
            ->set('guid', static fn () => Type::string())
33
            ->set('json', static fn () => new Json())
34
            ->set('date', static fn () => new Date())
35
            ->set('datetime', static fn () => new DateTime())
36
            ->set('datetimetz', static fn () => new DateTimeTZ())
37
            ->set('time', static fn () => new Time())
38
            ->set('date_immutable', static fn () => new DateImmutable())
39
            ->set('datetime_immutable', static fn () => new DateTimeImmutable())
40
            ->set('datetimetz_immutable', static fn () => new DateTimeTZImmutable())
41
            ->set('time_immutable', static fn () => new TimeImmutable())
42
            ->set('pageinfo', static fn () => new PageInfo())
43
            ->set('pagination', static fn () => new Pagination());
44
    }
45
46
    /** Use Type as return type */
47
    public function get(string $id): mixed
48
    {
49
        return parent::get($id);
50
    }
51
52
    /**
53
     * @param mixed[] $params
54
     *
55
     * @throws Error
56
     * @throws ReflectionException
57
     */
58
    public function build(string $typeClassName, string $typeName, mixed ...$params): Type
59
    {
60
        if ($this->has($typeName)) {
61
            return $this->get($typeName);
62
        }
63
64
        assert((new ReflectionClass($typeClassName))->implementsInterface(Buildable::class));
65
66
        return $this
67
            ->set($typeName, new $typeClassName($this, $typeName, $params))
68
            ->get($typeName);
69
    }
70
}
71