AbstractQueryHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 36
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 2
1
<?php
2
3
/*
4
 * cqrs (https://github.com/phpgears/cqrs).
5
 * CQRS base.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/cqrs
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\CQRS;
15
16
use Gears\CQRS\Exception\InvalidQueryException;
17
use Gears\DTO\DTO;
18
19
abstract class AbstractQueryHandler implements QueryHandler
20
{
21
    /**
22
     * {@inheritdoc}
23
     *
24
     * @throws InvalidQueryException
25
     */
26
    final public function handle(Query $query): DTO
27
    {
28
        if ($query->getQueryType() !== $this->getSupportedQueryType()) {
29
            throw new InvalidQueryException(\sprintf(
30
                'Query handler "%s" can only handle "%s" queries, "%s" given.',
31
                static::class,
32
                $this->getSupportedQueryType(),
33
                $query->getQueryType()
34
            ));
35
        }
36
37
        return $this->handleQuery($query);
38
    }
39
40
    /**
41
     * Get supported query type.
42
     *
43
     * @return string
44
     */
45
    abstract protected function getSupportedQueryType(): string;
46
47
    /**
48
     * Handle query.
49
     *
50
     * @param Query $query
51
     *
52
     * @return DTO
53
     */
54
    abstract protected function handleQuery(Query $query): DTO;
55
}
56