|
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
|
2 |
|
public static function create( |
|
21
|
|
|
LoopInterface $loop, |
|
22
|
|
|
array $options = [] |
|
23
|
|
|
): Client { |
|
24
|
2 |
|
return new Client( |
|
25
|
2 |
|
self::createContainer($loop, $options) |
|
26
|
|
|
); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
private static function createContainer( |
|
30
|
|
|
LoopInterface $loop, |
|
31
|
|
|
array $options |
|
32
|
|
|
): ContainerInterface { |
|
33
|
2 |
|
$builder = new ContainerBuilder(); |
|
34
|
|
|
|
|
35
|
2 |
|
$builder->addDefinitions([ |
|
36
|
2 |
|
LoopInterface::class => $loop, |
|
37
|
2 |
|
Locator::class => function (ContainerInterface $container) { |
|
38
|
|
|
return new ContainerLocator($container); |
|
39
|
2 |
|
}, |
|
40
|
2 |
|
TransportClientInterface::class => function ( |
|
41
|
|
|
Locator $locator, |
|
42
|
|
|
LoopInterface $loop |
|
43
|
|
|
) use ($options) { |
|
44
|
|
|
return self::createTransport($locator, $loop, $options); |
|
45
|
2 |
|
}, |
|
46
|
2 |
|
Hydrator::class => function (LoopInterface $loop, CommandBusInterface $commandBus) use ($options) { |
|
47
|
|
|
return self::createHydrator($loop, $commandBus, $options); |
|
48
|
2 |
|
}, |
|
49
|
2 |
|
CommandBusInterface::class => function (ContainerInterface $container) { |
|
50
|
2 |
|
return CommandBusFactory::create($container); |
|
|
|
|
|
|
51
|
2 |
|
}, |
|
52
|
|
|
]); |
|
53
|
2 |
|
$builder->addDefinitions($options[Options::CONTAINER_DEFINITIONS] ?? []); |
|
54
|
|
|
|
|
55
|
2 |
|
return $builder->build(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
View Code Duplication |
private static function createTransport( |
|
|
|
|
|
|
59
|
|
|
Locator $locator, |
|
60
|
|
|
LoopInterface $loop, |
|
61
|
|
|
array $options = [] |
|
62
|
|
|
): TransportClientInterface { |
|
63
|
|
|
if (!isset($options[Options::TRANSPORT_OPTIONS])) { |
|
64
|
|
|
throw new InvalidArgumentException('Missing Transport options'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return TransportFactory::create($locator, $loop, $options[Options::TRANSPORT_OPTIONS]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
View Code Duplication |
private static function createHydrator(LoopInterface $loop, CommandBusInterface $commandBus, array $options = []) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
if (!isset($options[Options::HYDRATOR_OPTIONS])) { |
|
73
|
|
|
throw new InvalidArgumentException('Missing Hydrator options'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return HydratorFactory::create($loop, $commandBus, $options[Options::HYDRATOR_OPTIONS]); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.