Completed
Push — master ( 7c6dd2...665b11 )
by Jonathan
02:42
created

Dispatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 40
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A dispatch() 0 13 4
1
<?php
2
namespace Spekkionu\DomainDispatcher;
3
4
use League\Container\ContainerInterface;
5
6
class Dispatcher
7
{
8
    /**
9
     * @var ContainerInterface
10
     */
11
    private $container;
12
13
    /**
14
     * Dispatcher constructor.
15
     * @param ContainerInterface $container
16
     */
17 7
    public function __construct(ContainerInterface $container)
18
    {
19 7
        $this->container = $container;
20 7
    }
21
22
    /**
23
     * Dispatches command
24
     *
25
     * @param mixed $command The command to dispatch.  Can be a callable or object with a handle method.
26
     * @param array $args Array of arguments to pass to the command
27
     *
28
     * @return mixed
29
     * @throws \Interop\Container\Exception\ContainerException
30
     * @throws \InvalidArgumentException
31
     */
32 7
    public function dispatch($command, array $args = [])
33
    {
34 7
        if (is_callable($command)) {
35 2
            return call_user_func($command);
36
        }
37 5
        if (is_string($command)) {
38 1
            $command = $this->container->get($command);
39
        }
40 5
        if (!method_exists($command, 'handle')) {
41 1
            throw new \InvalidArgumentException('Command must be a callable or implement a handle method.');
42
        }
43 4
        return $this->container->call([$command, 'handle'], $args);
44
    }
45
}
46