Completed
Pull Request — master (#1)
by De Cramer
02:40 queued 40s
created

ChainBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerFactory() 0 3 1
A buildChainProcessor() 0 8 2
A getOperationFromConfig() 0 10 3
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