Completed
Push — master ( 9f1a25...922242 )
by De Cramer
10s
created

ChainBuilder::getOperationFromConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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