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

Dispatcher::dispatch()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 5
nop 2
crap 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