1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smart\EtlBundle\Extractor; |
4
|
|
|
|
5
|
|
|
use Smart\EtlBundle\Exception\Extractor\EntityAlreadyRegisteredException; |
6
|
|
|
use Smart\EtlBundle\Exception\Extractor\EntityIdentifiedNotFoundException; |
7
|
|
|
use Smart\EtlBundle\Exception\Extractor\EntityIdentifierAlreadyProcessedException; |
8
|
|
|
use Smart\EtlBundle\Exception\Extractor\EntityTypeNotHandledException; |
9
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Nicolas Bastien <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
trait EntityExtractorTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* List of entities to extract |
18
|
|
|
* [ |
19
|
|
|
* 'entity_code' => 'Model Classname' |
20
|
|
|
* ] |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $entitiesToProcess = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $entities = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var PropertyAccessor |
32
|
|
|
*/ |
33
|
|
|
protected $accessor; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $entityType |
37
|
|
|
* @param string$entityClass |
38
|
|
|
* @param function $identifierCallback |
39
|
|
|
* @return $this |
40
|
|
|
* @throws \Exception |
41
|
|
|
*/ |
42
|
5 |
|
public function addEntityToProcess($entityType, $entityClass, $identifierCallback) |
43
|
|
|
{ |
44
|
5 |
|
if (isset($this->entitiesToProcess[$entityType])) { |
45
|
1 |
|
throw new EntityAlreadyRegisteredException($entityType); |
46
|
|
|
} |
47
|
|
|
|
48
|
5 |
|
$this->entitiesToProcess[$entityType] = [ |
49
|
5 |
|
'type' => $entityType, |
50
|
5 |
|
'class' => $entityClass, |
51
|
5 |
|
'callback' => $identifierCallback |
52
|
|
|
]; |
53
|
|
|
|
54
|
5 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
4 |
|
public function extract() |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
4 |
|
$this->init(); |
64
|
4 |
|
$this->check(); |
65
|
|
|
|
66
|
4 |
|
$files = $this->getFiles($this->getFileExtension()); |
|
|
|
|
67
|
4 |
|
foreach ($this->entitiesToProcess as $entityType => $data) { |
68
|
4 |
|
if (!isset($files[$entityType])) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
4 |
|
$this->processFile($entityType); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
return $this->entities; |
75
|
2 |
|
} catch (\Exception $e) { |
76
|
2 |
|
throw $e; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
4 |
|
protected function check() |
81
|
|
|
{ |
82
|
4 |
|
parent::check(); |
83
|
|
|
|
84
|
4 |
|
if (count($this->entitiesToProcess) === 0) { |
85
|
|
|
throw new \BadMethodCallException('Nothing to process'); |
86
|
|
|
} |
87
|
4 |
|
} |
88
|
|
|
|
89
|
4 |
|
protected function init() |
90
|
|
|
{ |
91
|
4 |
|
$this->accessor = PropertyAccess::createPropertyAccessor(); |
|
|
|
|
92
|
4 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $entityType |
96
|
|
|
* @param array $data |
97
|
|
|
* @return mixed |
98
|
|
|
* @throws \Exception |
99
|
|
|
*/ |
100
|
4 |
|
protected function processObject($entityType, array $data) |
101
|
|
|
{ |
102
|
4 |
|
if (!isset($this->entitiesToProcess[$entityType])) { |
103
|
|
|
throw new EntityTypeNotHandledException($entityType); |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
$objectClass = $this->entitiesToProcess[$entityType]['class']; |
107
|
4 |
|
$object = new $objectClass(); |
108
|
|
|
|
109
|
4 |
|
foreach ($data as $key => $value) { |
110
|
4 |
|
$valueToSet = $value; |
111
|
4 |
|
if (strpos($value, '@') === 0) { |
112
|
|
|
//handle relations |
113
|
3 |
|
$valueToSet = $this->getEntity($value); |
114
|
|
|
} |
115
|
|
|
|
116
|
4 |
|
$this->accessor->setValue($object, $key, $valueToSet); |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
return $object; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $identifier |
124
|
|
|
* @return mixed |
125
|
|
|
* @throws \Exception |
126
|
|
|
*/ |
127
|
3 |
|
protected function getEntity($identifier) |
128
|
|
|
{ |
129
|
3 |
|
if (strpos($identifier, '@') === 0) { |
130
|
3 |
|
$identifier = substr($identifier, 1); |
131
|
|
|
} |
132
|
3 |
|
if (!isset($this->entities[$identifier])) { |
133
|
1 |
|
throw new EntityIdentifiedNotFoundException($identifier); |
134
|
|
|
} |
135
|
|
|
|
136
|
2 |
|
return $this->entities[$identifier]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $filename |
141
|
|
|
* @throws \Exception |
142
|
|
|
*/ |
143
|
4 |
|
protected function processFile($filename) |
144
|
|
|
{ |
145
|
4 |
|
$filepath = sprintf('%s/%s.' . $this->getFileExtension(), $this->folderToExtract, $filename); |
|
|
|
|
146
|
|
|
|
147
|
4 |
|
$data = $this->extractFileContent($filepath); |
|
|
|
|
148
|
4 |
|
foreach ($data as $values) { |
149
|
4 |
|
$object = $this->processObject($filename, $values); |
150
|
3 |
|
if ($object !== null) { |
151
|
3 |
|
$entityIdentifier = $this->entitiesToProcess[$filename]['callback']($object); |
152
|
3 |
|
if (isset($this->entities[$entityIdentifier])) { |
153
|
1 |
|
throw new EntityIdentifierAlreadyProcessedException($entityIdentifier); |
154
|
|
|
} |
155
|
3 |
|
$this->entities[$entityIdentifier] = $object; |
156
|
|
|
} |
157
|
|
|
} |
158
|
2 |
|
} |
159
|
|
|
} |
160
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.