1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Foundation; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Hydrator\Factory as HydratorFactory; |
6
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
7
|
|
|
use ApiClients\Foundation\Middleware\Locator\ContainerLocator; |
8
|
|
|
use ApiClients\Foundation\Middleware\Locator\Locator; |
9
|
|
|
use ApiClients\Foundation\Transport\ClientInterface as TransportClientInterface; |
10
|
|
|
use ApiClients\Foundation\Transport\Factory as TransportFactory; |
11
|
|
|
use ApiClients\Tools\CommandBus\CommandBusInterface; |
12
|
|
|
use ApiClients\Tools\CommandBus\Factory as CommandBusFactory; |
13
|
|
|
use DI\ContainerBuilder; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use Psr\Container\ContainerInterface; |
16
|
|
|
use React\EventLoop\LoopInterface; |
17
|
|
|
|
18
|
|
|
final class Factory |
19
|
|
|
{ |
20
|
3 |
|
public static function create( |
21
|
|
|
LoopInterface $loop, |
22
|
|
|
array $options = [] |
23
|
|
|
): Client { |
24
|
3 |
|
return new Client( |
25
|
3 |
|
self::createContainer($loop, $options) |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
3 |
|
private static function createContainer( |
30
|
|
|
LoopInterface $loop, |
31
|
|
|
array $options |
32
|
|
|
): ContainerInterface { |
33
|
3 |
|
$builder = new ContainerBuilder(); |
34
|
|
|
|
35
|
3 |
|
$builder->addDefinitions([ |
36
|
3 |
|
LoopInterface::class => $loop, |
37
|
3 |
|
Locator::class => function (ContainerInterface $container) { |
38
|
2 |
|
return new ContainerLocator($container); |
39
|
3 |
|
}, |
40
|
3 |
|
TransportClientInterface::class => function ( |
41
|
|
|
Locator $locator, |
42
|
|
|
LoopInterface $loop |
43
|
|
|
) use ($options) { |
44
|
2 |
|
return self::createTransport($locator, $loop, $options); |
45
|
3 |
|
}, |
46
|
3 |
|
Hydrator::class => function (LoopInterface $loop, CommandBusInterface $commandBus) use ($options) { |
47
|
2 |
|
return self::createHydrator($loop, $commandBus, $options); |
48
|
3 |
|
}, |
49
|
3 |
|
CommandBusInterface::class => function (ContainerInterface $container) use ($options) { |
50
|
3 |
|
return CommandBusFactory::create($container, $options[Options::COMMAND_BUS_OPTIONS] ?? []); |
51
|
3 |
|
}, |
52
|
|
|
]); |
53
|
3 |
|
$builder->addDefinitions($options[Options::CONTAINER_DEFINITIONS] ?? []); |
54
|
|
|
|
55
|
3 |
|
return $builder->build(); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
View Code Duplication |
private static function createTransport( |
|
|
|
|
59
|
|
|
Locator $locator, |
60
|
|
|
LoopInterface $loop, |
61
|
|
|
array $options = [] |
62
|
|
|
): TransportClientInterface { |
63
|
2 |
|
if (!isset($options[Options::TRANSPORT_OPTIONS])) { |
64
|
1 |
|
throw new InvalidArgumentException('Missing Transport options'); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
return TransportFactory::create($locator, $loop, $options[Options::TRANSPORT_OPTIONS]); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
View Code Duplication |
private static function createHydrator(LoopInterface $loop, CommandBusInterface $commandBus, array $options = []) |
|
|
|
|
71
|
|
|
{ |
72
|
2 |
|
if (!isset($options[Options::HYDRATOR_OPTIONS])) { |
73
|
1 |
|
throw new InvalidArgumentException('Missing Hydrator options'); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return HydratorFactory::create($loop, $commandBus, $options[Options::HYDRATOR_OPTIONS]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.