Completed
Pull Request — master (#1)
by De Cramer
02:11
created

ChainBuilder::buildChainProcessor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oliverde8\Component\PhpEtl;
4
5
use Oliverde8\Component\PhpEtl\Builder\Factories\AbstractFactory;
6
7
/**
8
 * Class ChainBuilder
9
 *
10
 * @author    de Cramer Oliver<[email protected]>
11
 * @copyright 2018 Oliverde8
12
 * @package Oliverde8\Component\PhpEtl
13
 */
14
class ChainBuilder
15
{
16
    /** @var AbstractFactory[] */
17
    protected $operationFactories;
18
19
    /**
20
     * Register a operation factory.
21
     *
22
     * @param AbstractFactory $factory
23
     */
24
    public function registerFactory(AbstractFactory $factory)
25
    {
26
        $this->operationFactories[] = $factory;
27
    }
28
29
    /**
30
     * @param $configs
31
     *
32
     * @return ChainProcessorInterface
33
     */
34
    public function buildChainProcessor($configs)
35
    {
36
        $chainOperations = [];
37
        foreach ($configs as $id => $operation) {
38
            $chainOperations[$id] = $this->getOperationFromConfig($operation);
39
        }
40
41
        return new ChainProcessor($chainOperations);
42
    }
43
44
    /**
45
     * @param $config
46
     *
47
     * @return null|ChainOperation\ChainOperationInterface
48
     */
49
    protected function getOperationFromConfig($config)
50
    {
51
        foreach ($this->operationFactories as $factory) {
52
            if ($factory->supports($config['operation'], $config['options'])) {
53
                return $factory->getOperation($config['operation'], $config['options']);
54
            }
55
        }
56
57
        // TODO handle as error.
58
        return null;
59
    }
60
}
61