|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maketok\DataMigration\Action\Type; |
|
4
|
|
|
|
|
5
|
|
|
use Maketok\DataMigration\Action\ActionInterface; |
|
6
|
|
|
use Maketok\DataMigration\Action\Exception\WrongContextException; |
|
7
|
|
|
use Maketok\DataMigration\Unit\ImportDbUnitInterface; |
|
8
|
|
|
use Maketok\DataMigration\Unit\ImportFileUnitInterface; |
|
9
|
|
|
use Maketok\DataMigration\Unit\UnitBagInterface; |
|
10
|
|
|
use Maketok\DataMigration\Workflow\ResultInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Load data from tmp files to tmp tables |
|
14
|
|
|
*/ |
|
15
|
|
|
class Load extends AbstractDbAction implements ActionInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var UnitBagInterface|ImportDbUnitInterface[]|ImportFileUnitInterface[] |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $bag; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
* @throws WrongContextException |
|
25
|
|
|
*/ |
|
26
|
7 |
|
public function process(ResultInterface $result) |
|
27
|
|
|
{ |
|
28
|
7 |
|
$result->setActionStartTime($this->getCode(), new \DateTime()); |
|
29
|
7 |
|
foreach ($this->bag as $unit) { |
|
30
|
7 |
|
if ($unit->getTmpFileName() === null) { |
|
31
|
1 |
|
throw new WrongContextException(sprintf( |
|
32
|
1 |
|
"Action can not be used for current unit %s. Tmp file is missing.", |
|
33
|
1 |
|
$unit->getCode() |
|
34
|
1 |
|
)); |
|
35
|
|
|
} |
|
36
|
6 |
|
$unit->setTmpTable($this->getTmpTableName($unit)); |
|
37
|
6 |
|
$values = array_map(function () { |
|
38
|
5 |
|
return ['text', ['notnull' => false]]; |
|
39
|
6 |
|
}, $unit->getMapping()); |
|
40
|
6 |
|
$this->resource->createTmpTable( |
|
41
|
6 |
|
$unit->getTmpTable(), |
|
42
|
6 |
|
array_combine(array_keys($unit->getMapping()), $values) |
|
43
|
6 |
|
); |
|
44
|
6 |
|
$loaded = $this->resource->loadData( |
|
45
|
6 |
|
$unit->getTmpTable(), |
|
46
|
6 |
|
$unit->getTmpFileName(), |
|
47
|
6 |
|
$this->config->offsetGet('local_infile') |
|
48
|
6 |
|
); |
|
49
|
6 |
|
$result->incrementActionProcessed($this->getCode(), $loaded); |
|
50
|
6 |
|
if (!$this->config['file_debug']) { |
|
51
|
6 |
|
$unit->getFilesystem()->cleanUp($unit->getTmpFileName()); |
|
52
|
6 |
|
} |
|
53
|
6 |
|
} |
|
54
|
6 |
|
$result->setActionEndTime($this->getCode(), new \DateTime()); |
|
55
|
6 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
8 |
|
public function getCode() |
|
61
|
|
|
{ |
|
62
|
8 |
|
return 'load'; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|