1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Foundation; |
4
|
|
|
|
5
|
|
|
use Acclimate\Container\CompositeContainer; |
6
|
|
|
use ApiClients\Foundation\Hydrator\Factory as HydratorFactory; |
7
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
8
|
|
|
use ApiClients\Foundation\Middleware\Locator\ContainerLocator; |
9
|
|
|
use ApiClients\Foundation\Middleware\Locator\Locator; |
10
|
|
|
use ApiClients\Foundation\Transport\ClientInterface as TransportClientInterface; |
11
|
|
|
use ApiClients\Foundation\Transport\Factory as TransportFactory; |
12
|
|
|
use ApiClients\Tools\CommandBus\CommandBusInterface; |
13
|
|
|
use ApiClients\Tools\CommandBus\Factory as CommandBusFactory; |
14
|
|
|
use DI\ContainerBuilder; |
15
|
|
|
use Psr\Container\ContainerInterface; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use React\EventLoop\LoopInterface; |
18
|
|
|
|
19
|
|
|
final class Factory |
20
|
|
|
{ |
21
|
|
|
public static function create( |
22
|
|
|
LoopInterface $loop, |
23
|
|
|
array $options = [] |
24
|
|
|
): Client { |
25
|
|
|
return new Client( |
26
|
|
|
self::createContainer($loop, $options) |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
private static function createContainer( |
30
|
|
|
LoopInterface $loop, |
31
|
|
|
array $options |
32
|
|
|
): ContainerInterface { |
33
|
|
|
$builder = new ContainerBuilder(); |
34
|
|
|
|
35
|
|
|
$builder->addDefinitions([ |
36
|
|
|
LoopInterface::class => $loop, |
37
|
|
|
Locator::class => function (ContainerInterface $container) { |
38
|
|
|
return new ContainerLocator($container); |
39
|
|
|
}, |
40
|
|
|
TransportClientInterface::class => function ( |
41
|
|
|
Locator $locator, |
42
|
|
|
LoopInterface $loop |
43
|
|
|
) use ($options) { |
44
|
|
|
return self::createTransport($locator, $loop, $options); |
45
|
|
|
}, |
46
|
|
|
Hydrator::class => function (LoopInterface $loop, CommandBusInterface $commandBus) use ($options) { |
47
|
|
|
return self::createHydrator($loop, $commandBus, $options); |
48
|
|
|
}, |
49
|
|
|
CommandBusInterface::class => function (ContainerInterface $container) { |
50
|
|
|
return CommandBusFactory::create($container); |
|
|
|
|
51
|
|
|
}, |
52
|
|
|
]); |
53
|
|
|
$builder->addDefinitions($options[Options::CONTAINER_DEFINITIONS] ?? []); |
54
|
|
|
|
55
|
|
|
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.