Completed
Push — master ( e274d8...0f3489 )
by Cees-Jan
11:36 queued 10:24
created

src/Factory.php (1 issue)

Upgrade to new PHP Analysis Engine

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\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] ?? []);
0 ignored issues
show
The call to Factory::create() has too many arguments starting with $options[\ApiClients\Fou...BUS_OPTIONS] ?? array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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