AbstractFactory::makeHandler()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 7
nop 1
crap 2.0116
1
<?php
2
3
namespace Nayjest\Querying\Handler;
4
5
use InvalidArgumentException;
6
7
use Nayjest\Querying\Handler\PostProcess\AddFieldHandler;
8
use Nayjest\Querying\Operation\AddFieldOperation;
9
use Nayjest\Querying\Operation\CloneOperation;
10
use Nayjest\Querying\Operation\CustomOperation;
11
use Nayjest\Querying\Operation\DummyOperation;
12
use Nayjest\Querying\Operation\InitializeRowsOperation;
13
use Nayjest\Querying\Operation\OperationInterface;
14
15
abstract class AbstractFactory
16
{
17
    /**
18
     * @return array
19
     */
20
    abstract protected function getRegisteredHandlers();
21
22 11
    protected function getDefaultHandlers()
23
    {
24
        return [
25 11
            DummyOperation::class => DummyHandler::class,
26 11
            InitializeRowsOperation::class => InitializeRowsHandler::class,
27 11
            AddFieldOperation::class => AddFieldHandler::class,
28 11
            CustomOperation::class => CustomHandler::class,
29 11
            CloneOperation::class => CloneHandler::class,
30 11
        ];
31
    }
32
33 11
    final protected function getHandlers()
34
    {
35 11
        return array_merge(
36 11
            $this->getDefaultHandlers(),
37 11
            $this->getRegisteredHandlers()
38 11
        );
39
    }
40
41 11
    public function makeHandler(OperationInterface $operation)
42
    {
43 11
        $operationClass = get_class($operation);
44 11
        $classes = $this->getHandlers();
45 11
        if (array_key_exists($operationClass, $classes)) {
46 11
            $handlerClass = $classes[$operationClass];
47 11
            return new $handlerClass($operation);
48
        }
49
        throw new InvalidArgumentException("No handler for '$operationClass' operation.");
50
    }
51
}
52