Passed
Pull Request — main (#133)
by Tom
03:39
created

Services::__construct()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 100
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 68
nc 1
nop 3
dl 0
loc 100
rs 8.6981
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\GraphQL\Resolve;
6
7
use ApiSkeletons\Doctrine\GraphQL\Config;
8
use ApiSkeletons\Doctrine\GraphQL\Criteria;
9
use ApiSkeletons\Doctrine\GraphQL\Hydrator;
10
use ApiSkeletons\Doctrine\GraphQL\Input;
11
use ApiSkeletons\Doctrine\GraphQL\Metadata;
12
use ApiSkeletons\Doctrine\GraphQL\Resolve;
13
use ApiSkeletons\Doctrine\GraphQL\Type;
14
use Doctrine\ORM\EntityManager;
15
use League\Event\EventDispatcher;
16
use Psr\Container\ContainerInterface;
17
18
/**
19
 * This trait is used to remove complexity from the Driver class.
20
 * It doesn't change what the Driver does.  It just separates the container work
21
 * from the GraphQL Driver.
22
 */
23
trait Services
24
{
25
    /**
26
     * @param string                 $entityManagerAlias required
27
     * @param Config                 $config             required
28
     * @param Metadata\Metadata|null $metadata           optional so cached metadata can be loaded
29
     */
30
    public function __construct(
31
        EntityManager $entityManager,
32
        Config|null $config = null,
33
        array|null $metadataConfig = null,
34
    ) {
35
        $this
36
            // Plain classes
37
            ->set(EntityManager::class, $entityManager)
38
            ->set(
39
                Config::class,
40
                static function () use ($config) {
41
                    if (! $config) {
42
                        $config = new Config();
43
                    }
44
45
                    return $config;
46
                },
47
            )
48
            ->set(
49
                EventDispatcher::class,
50
                static fn () => new EventDispatcher()
51
            )
52
            ->set(
53
                Type\TypeManager::class,
54
                static fn (ContainerInterface $container) => new Type\TypeManager($container)
55
            )
56
            ->set(
57
                Metadata\Metadata::class,
58
                static function (ContainerInterface $container) use ($metadataConfig) {
59
                    return (new Metadata\MetadataFactory($container, $metadataConfig))->getMetadata();
60
                },
61
            )
62
            ->set(
63
                Metadata\GlobalEnable::class,
64
                static function (ContainerInterface $container) {
65
                    return new Metadata\GlobalEnable(
66
                        $container->get(EntityManager::class),
67
                        $container->get(Config::class),
68
                    );
69
                },
70
            )
71
            ->set(
72
                Resolve\FieldResolver::class,
73
                static function (ContainerInterface $container) {
74
                    return new Resolve\FieldResolver(
75
                        $container->get(Config::class),
76
                        $container->get(Type\TypeManager::class),
77
                    );
78
                },
79
            )
80
            ->set(
81
                Resolve\ResolveCollectionFactory::class,
82
                static function (ContainerInterface $container) {
83
                    return new Resolve\ResolveCollectionFactory(
84
                        $container->get(EntityManager::class),
85
                        $container->get(Config::class),
86
                        $container->get(Resolve\FieldResolver::class),
87
                        $container->get(Type\TypeManager::class),
88
                        $container->get(EventDispatcher::class),
89
                        $container->get(Metadata\Metadata::class),
90
                    );
91
                },
92
            )
93
            ->set(
94
                Resolve\ResolveEntityFactory::class,
95
                static function (ContainerInterface $container) {
96
                    return new Resolve\ResolveEntityFactory(
97
                        $container->get(Config::class),
98
                        $container->get(EntityManager::class),
99
                        $container->get(EventDispatcher::class),
100
                    );
101
                },
102
            )
103
            ->set(
104
                Criteria\CriteriaFactory::class,
105
                static function (ContainerInterface $container) {
106
                    return new Criteria\CriteriaFactory(
107
                        $container->get(Config::class),
108
                        $container->get(EntityManager::class),
109
                        $container->get(Type\TypeManager::class),
110
                        $container->get(EventDispatcher::class),
111
                    );
112
                },
113
            )
114
            ->set(
115
                Hydrator\HydratorFactory::class,
116
                static function (ContainerInterface $container) {
117
                    return new Hydrator\HydratorFactory(
118
                        $container->get(EntityManager::class),
119
                        $container->get(Type\TypeManager::class),
120
                    );
121
                },
122
            )
123
            ->set(
124
                Input\InputFactory::class,
125
                static function (ContainerInterface $container) {
126
                    return new Input\InputFactory(
127
                        $container->get(Config::class),
128
                        $container->get(EntityManager::class),
129
                        $container->get(Type\TypeManager::class),
130
                    );
131
                },
132
            );
133
    }
134
135
    abstract public function set(string $id, mixed $value): mixed;
136
}
137