Completed
Pull Request — master (#2)
by lee
06:59
created

HandlerLocatedQueryBus::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Query\Bus;
12
13
use GpsLab\Component\Query\Exception\HandlerNotFoundException;
14
use GpsLab\Component\Query\Handler\Locator\QueryHandlerLocator;
15
use GpsLab\Component\Query\Query;
16
17
class HandlerLocatedQueryBus implements QueryBus
18
{
19
    /**
20
     * @var QueryHandlerLocator
21
     */
22
    private $locator;
23
24
    /**
25
     * @param QueryHandlerLocator $locator
26
     */
27
    public function __construct(QueryHandlerLocator $locator)
28
    {
29
        $this->locator = $locator;
30
    }
31
32
    /**
33
     * @param Query $query
34
     *
35
     * @return mixed
36
     */
37
    public function handle(Query $query)
38
    {
39
        $handler = $this->locator->findHandler($query);
40
41
        if (!is_callable($handler)) {
42
            throw HandlerNotFoundException::notFound($query);
43
        }
44
45
        return call_user_func($handler, $query);
46
    }
47
}
48