These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace ApiClients\Foundation; |
||
4 | |||
5 | use ApiClients\Foundation\Events\CommandLocatorEvent; |
||
6 | use ApiClients\Foundation\Hydrator\Factory as HydratorFactory; |
||
7 | use ApiClients\Foundation\Hydrator\Hydrator; |
||
8 | use ApiClients\Foundation\Transport\Client as TransportClient; |
||
9 | use ApiClients\Foundation\Transport\Factory as TransportFactory; |
||
10 | use ApiClients\Tools\CommandBus\CommandBus; |
||
11 | use Interop\Container\ContainerInterface; |
||
12 | use League\Container\Container; |
||
13 | use League\Container\ReflectionContainer; |
||
14 | use League\Event\Emitter; |
||
15 | use League\Event\EmitterInterface; |
||
16 | use League\Tactician\Container\ContainerLocator; |
||
17 | use League\Tactician\Handler\CommandHandlerMiddleware; |
||
18 | use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor; |
||
19 | use League\Tactician\Handler\MethodNameInflector\HandleInflector; |
||
20 | use React\EventLoop\LoopInterface; |
||
21 | |||
22 | final class Factory |
||
23 | { |
||
24 | public static function create( |
||
25 | LoopInterface $loop = null, |
||
26 | ContainerInterface $wrappedContainer = null, |
||
27 | array $options = [] |
||
28 | ): Client { |
||
29 | $container = self::createContainer($wrappedContainer); |
||
30 | |||
31 | $container->share(EmitterInterface::class, new Emitter()); |
||
32 | $container->share(TransportClient::class, self::createTransport($container, $loop, $options)); |
||
33 | $container->share(Hydrator::class, self::createHydrator($container, $options)); |
||
34 | $container->share(CommandBus::class, function () use ($container) { |
||
35 | return self::createCommandBus($container); |
||
36 | }); |
||
37 | |||
38 | return new Client( |
||
39 | $container |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | private static function createContainer(ContainerInterface $wrappedContainer = null): Container |
||
44 | { |
||
45 | $container = new Container(); |
||
46 | $container->delegate(new ReflectionContainer()); |
||
47 | |||
48 | if ($wrappedContainer instanceof ContainerInterface) { |
||
49 | $container->delegate($wrappedContainer); |
||
50 | } |
||
51 | |||
52 | return $container; |
||
53 | } |
||
54 | |||
55 | private static function createCommandBus(ContainerInterface $container): CommandBus |
||
56 | { |
||
57 | $commandToHandlerMap = self::mapCommandsToHandlers($container->get(EmitterInterface::class)); |
||
58 | |||
59 | $containerLocator = new ContainerLocator( |
||
60 | $container, |
||
61 | $commandToHandlerMap |
||
62 | ); |
||
63 | |||
64 | $commandHandlerMiddleware = new CommandHandlerMiddleware( |
||
65 | new ClassNameExtractor(), |
||
66 | $containerLocator, |
||
67 | new HandleInflector() |
||
68 | ); |
||
69 | |||
70 | return new CommandBus( |
||
71 | $container->get(LoopInterface::class), |
||
72 | $commandHandlerMiddleware |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | private static function mapCommandsToHandlers(EmitterInterface $emitter): array |
||
77 | { |
||
78 | return $emitter->emit(CommandLocatorEvent::create())->getMap(); |
||
0 ignored issues
–
show
|
|||
79 | } |
||
80 | |||
81 | private static function createTransport( |
||
82 | ContainerInterface $container, |
||
83 | LoopInterface $loop = null, |
||
84 | array $options = [] |
||
85 | ): TransportClient { |
||
86 | return TransportFactory::create($container, $loop, $options[Options::TRANSPORT_OPTIONS]); |
||
87 | } |
||
88 | |||
89 | private static function createHydrator(ContainerInterface $container, array $options = []) |
||
90 | { |
||
91 | if (isset($options[Options::HYDRATOR]) && $options[Options::HYDRATOR] instanceof Hydrator) { |
||
92 | return $options[Options::HYDRATOR]; |
||
93 | } |
||
94 | |||
95 | if (!isset($options[Options::HYDRATOR_OPTIONS])) { |
||
96 | throw new \Exception('Missing Hydrator options'); |
||
97 | } |
||
98 | |||
99 | return HydratorFactory::create($container, $options[Options::HYDRATOR_OPTIONS]); |
||
100 | } |
||
101 | } |
||
102 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: