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\Transport\Client as TransportClient; |
8
|
|
|
use ApiClients\Foundation\Transport\Factory as TransportFactory; |
9
|
|
|
use ApiClients\Tools\CommandBus\CommandBus; |
10
|
|
|
use ApiClients\Tools\CommandBus\Factory as CommandBusFactory; |
11
|
|
|
use DI\ContainerBuilder; |
12
|
|
|
use Interop\Container\ContainerInterface; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use League\Event\Emitter; |
15
|
|
|
use League\Event\EmitterInterface; |
16
|
|
|
use React\EventLoop\LoopInterface; |
17
|
|
|
|
18
|
|
|
final class Factory |
19
|
|
|
{ |
20
|
|
|
public static function create( |
21
|
|
|
LoopInterface $loop, |
22
|
|
|
array $options = [] |
23
|
|
|
): Client { |
24
|
|
|
return new Client( |
25
|
|
|
self::createContainer($loop, $options) |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private static function createContainer(LoopInterface $loop, array $options): ContainerInterface |
30
|
|
|
{ |
31
|
|
|
$container = new ContainerBuilder(); |
32
|
|
|
|
33
|
|
|
$container->addDefinitions([ |
34
|
|
|
EmitterInterface::class => new Emitter(), |
35
|
|
|
LoopInterface::class => $loop, |
36
|
|
|
TransportClient::class => function (ContainerInterface $container, LoopInterface $loop) use ($options) { |
37
|
|
|
return self::createTransport($container, $loop, $options); |
38
|
|
|
}, |
39
|
|
|
Hydrator::class => function (ContainerInterface $container) use ($options) { |
40
|
|
|
return self::createHydrator($container, $options); |
41
|
|
|
}, |
42
|
|
|
CommandBus::class => function (ContainerInterface $container) { |
43
|
|
|
return CommandBusFactory::create($container); |
44
|
|
|
}, |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
return $container->build(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
private static function createTransport( |
|
|
|
|
51
|
|
|
ContainerInterface $container, |
52
|
|
|
LoopInterface $loop, |
53
|
|
|
array $options = [] |
54
|
|
|
): TransportClient { |
55
|
|
|
if (!isset($options[Options::TRANSPORT_OPTIONS])) { |
56
|
|
|
throw new InvalidArgumentException('Missing Transport options'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return TransportFactory::create($container, $loop, $options[Options::TRANSPORT_OPTIONS]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
private static function createHydrator(ContainerInterface $container, array $options = []) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
if (!isset($options[Options::HYDRATOR_OPTIONS])) { |
65
|
|
|
throw new InvalidArgumentException('Missing Hydrator options'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return HydratorFactory::create($container, $options[Options::HYDRATOR_OPTIONS]); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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.