1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smart\EtlBundle\Extractor; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
6
|
|
|
use Symfony\Component\Yaml\Yaml; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Nicolas Bastien <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class YamlEntityExtractor extends AbstractFolderExtrator implements ExtractorInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* List of entities to extract |
15
|
|
|
* [ |
16
|
|
|
* 'entity_code' => 'Model Classname' |
17
|
|
|
* ] |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $processEntities; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $entities = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var PropertyAccessor |
29
|
|
|
*/ |
30
|
|
|
protected $accessor; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function getProcessEntities() |
36
|
|
|
{ |
37
|
|
|
return $this->processEntities; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $processEntities |
42
|
|
|
*/ |
43
|
|
|
public function setProcessEntities($processEntities) |
44
|
|
|
{ |
45
|
|
|
$this->processEntities = $processEntities; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
51
|
|
|
public function extract() |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
|
|
$this->check(); |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
$files = $this->getFiles(); |
|
|
|
|
58
|
|
|
foreach ($files as $filename) { |
59
|
|
|
$this->processFile($filename); |
60
|
|
|
} |
61
|
|
|
} catch (\Exception $e) { |
62
|
|
|
throw $e; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function check() |
67
|
|
|
{ |
68
|
|
|
parent::check(); |
69
|
|
|
|
70
|
|
|
if (count($this->processEntities) === 0) { |
71
|
|
|
throw new \BadMethodCallException('Nothing to process'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $filename |
77
|
|
|
*/ |
78
|
|
|
protected function processFile($filename) |
79
|
|
|
{ |
80
|
|
|
$filepath = sprintf('%s/%s.yml', $this->folderToExtract, $filename); |
81
|
|
|
|
82
|
|
|
$data = Yaml::parse(file_get_contents($filepath)); |
83
|
|
|
foreach ($data as $values) { |
84
|
|
|
$object = $this->processObject($filename, $values); |
85
|
|
|
if ($object !== null) { |
86
|
|
|
//todo add dynamic identifier instead of code |
87
|
|
|
$this->entities[$object->getCode()] = $object; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $entityType |
94
|
|
|
* @param array $data |
95
|
|
|
* @return mixed |
96
|
|
|
* @throws \Exception |
97
|
|
|
*/ |
98
|
|
|
protected function processObject($entityType, array $data) |
99
|
|
|
{ |
100
|
|
|
if (!isset($this->processEntities[$entityType])) { |
101
|
|
|
throw new \Exception('Entity type ' . $entityType . ' is not handled'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$objectClass = $this->processEntities[$entityType]; |
105
|
|
|
$object = new $objectClass(); |
106
|
|
|
|
107
|
|
|
foreach ($data as $key => $value) { |
108
|
|
|
$valueToSet = $value; |
109
|
|
|
if (strpos($value, '@') === 0) { |
110
|
|
|
//handle relations |
111
|
|
|
$valueToSet = $this->getEntity($value); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->accessor->setValue($object, $key, $valueToSet); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $object; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $identifier |
122
|
|
|
* @return mixed |
123
|
|
|
* @throws \Exception |
124
|
|
|
*/ |
125
|
|
|
protected function getEntity($identifier) |
126
|
|
|
{ |
127
|
|
|
if (strpos($identifier, '@') === 0) { |
128
|
|
|
$identifier = substr($identifier, 1); |
129
|
|
|
} |
130
|
|
|
if (!isset($this->entities[$identifier])) { |
131
|
|
|
throw new \Exception('Entity identifed by ' . $identifier . ' not found'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->entities[$identifier]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks for function calls that miss required arguments.