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
|
|
|
|