Bus   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 14
c 3
b 0
f 0
dl 0
loc 52
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A orchestrate() 0 8 2
A execute() 0 3 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Application Use Case Class
5
 * @author Ticaje <[email protected]>
6
 */
7
8
namespace Ticaje\Hexagonal\Application\UseCase\Bus;
9
10
use Ticaje\Hexagonal\Application\Signatures\Responder\ResponseInterface;
11
use Ticaje\Hexagonal\Application\Signatures\UseCase\BusFacadeInterface;
12
use Ticaje\Hexagonal\Application\Signatures\UseCase\ImplementorInterface;
13
use Ticaje\Hexagonal\Application\Signatures\UseCase\UseCaseCommandInterface;
14
use League\Tactician\CommandBus;
15
16
/**
17
 * Class Bus
18
 * @package Ticaje\Hexagonal\Application\UseCase\Bus
19
 * This is the wrapper, the facade to our API that exposes service contract to modules using Command-Bus approach under a Hexagonal context.
20
 */
21
class Bus implements BusFacadeInterface
22
{
23
    /** @var ImplementorInterface */
24
    private ImplementorInterface $implementor;
25
26
    /** @var array $commands */
27
    private array $commands;
28
29
    /** @var array $handlers */
30
    private array $handlers;
31
32
    /** @var ?CommandBus $bus */
33
    private ?CommandBus $bus;
34
35
    /**
36
     * Bus constructor.
37
     *
38
     * @param array                $commands
39
     * @param array                $handlers
40
     * @param ImplementorInterface $implementor
41
     */
42
    public function __construct(
43
        array $commands,
44
        array $handlers,
45
        ImplementorInterface $implementor
46
    ) {
47
        $this->commands = $commands;
48
        $this->handlers = $handlers;
49
        $this->implementor = $implementor;
50
        $this->bus = $this->implementor->provide($this->orchestrate());
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function execute(UseCaseCommandInterface $command): ResponseInterface
57
    {
58
        return $this->bus->handle($command);
0 ignored issues
show
Bug introduced by
The method handle() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        return $this->bus->/** @scrutinizer ignore-call */ handle($command);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
    }
60
61
    /**
62
     * @return array
63
     * Orchestrating service contract, no virtual types allowed
64
     */
65
    private function orchestrate(): array
66
    {
67
        $result = [];
68
        foreach ($this->commands as $index => $command) {
69
            $result[get_class($command)] = $this->handlers[$index];
70
        }
71
72
        return $result;
73
    }
74
}
75