QueryInflector::inflect()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 19
rs 9.6111
cc 5
nc 3
nop 2
1
<?php
2
3
/*
4
 * cqrs-tactician (https://github.com/phpgears/cqrs-tactician).
5
 * CQRS implementation with League Tactician.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/cqrs-tactician
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\CQRS\Tactician;
15
16
use Gears\CQRS\Exception\InvalidQueryException;
17
use Gears\CQRS\Exception\InvalidQueryHandlerException;
18
use Gears\CQRS\Query;
19
use Gears\CQRS\QueryHandler;
20
use League\Tactician\Handler\MethodNameInflector\MethodNameInflector;
21
22
final class QueryInflector implements MethodNameInflector
23
{
24
    /**
25
     * Return the method name to call on the command handler and return it.
26
     *
27
     * @param mixed $command
28
     * @param mixed $handler
29
     *
30
     * @throws InvalidQueryException
31
     * @throws InvalidQueryHandlerException
32
     */
33
    public function inflect($command, $handler): string
34
    {
35
        if (!$command instanceof Query) {
36
            throw new InvalidQueryException(\sprintf(
37
                'Query must implement "%s" interface, "%s" given.',
38
                Query::class,
39
                \is_object($command) ? \get_class($command) : \gettype($command)
40
            ));
41
        }
42
43
        if (!$handler instanceof QueryHandler) {
44
            throw new InvalidQueryHandlerException(\sprintf(
45
                'Query handler must implement "%s" interface, "%s" given.',
46
                QueryHandler::class,
47
                \is_object($handler) ? \get_class($handler) : \gettype($handler)
48
            ));
49
        }
50
51
        return 'handle';
52
    }
53
}
54