AbstractFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 37
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
getRegisteredHandlers() 0 1 ?
A getDefaultHandlers() 0 10 1
A getHandlers() 0 7 1
A makeHandler() 0 10 2
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