Dump::start()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 9.7998
cc 3
nc 3
nop 0
crap 3
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\ExportDbUnitInterface;
8
use Maketok\DataMigration\Unit\ImportDbUnitInterface;
9
use Maketok\DataMigration\Unit\ImportFileUnitInterface;
10
use Maketok\DataMigration\Unit\UnitBagInterface;
11
use Maketok\DataMigration\Workflow\ResultInterface;
12
13
/**
14
 * Dump data from tmp table to tmp file
15
 */
16
class Dump extends AbstractDbAction implements ActionInterface
17
{
18
    /**
19
     * @var UnitBagInterface|ExportDbUnitInterface[]|ImportFileUnitInterface[]|ImportDbUnitInterface[]
20
     */
21
    protected $bag;
22
    /**
23
     * @var ResultInterface
24
     */
25
    protected $result;
26
27
28
    /**
29
     * {@inheritdoc}
30
     * @throws WrongContextException
31
     */
32 4
    public function process(ResultInterface $result)
33
    {
34 4
        $limit = $this->config->offsetGet('dump_limit');
35 4
        $this->result = $result;
36
        try {
37 4
            $this->start();
38 3
            foreach ($this->bag as $unit) {
39 3
                $offset = 0;
40 3
                while (($data = $this->resource->dumpData($unit->getTmpTable(),
41 3
                        array_keys($unit->getMapping()),
42 3
                        $limit,
43 3
                        $offset)) !== false && !empty($data)) {
44 3
                    $offset += $limit;
45 3
                    foreach ($data as $row) {
46 3
                        $unit->getFilesystem()->writeRow($row);
47 3
                        $result->incrementActionProcessed($this->getCode());
48 3
                    }
49 3
                }
50 3
            }
51 4
        } catch (\Exception $e) {
52 1
            $this->close();
53 1
            throw $e;
54
        }
55 3
        $this->close();
56 3
    }
57
58
    /**
59
     * open handlers
60
     * @throws WrongContextException
61
     */
62 4
    private function start()
63
    {
64 4
        $this->result->setActionStartTime($this->getCode(), new \DateTime());
65 4
        foreach ($this->bag as $unit) {
66 4
            if ($unit->getTmpTable() === null) {
67 1
                throw new WrongContextException(sprintf(
68 1
                    "Action can not be used for current unit %s. Tmp table is missing.",
69 1
                    $unit->getCode()
70 1
                ));
71
            }
72 3
            $unit->setTmpFileName($this->getTmpFileName($unit));
73 3
            $unit->getFilesystem()->open($unit->getTmpFileName(), 'w');
74 3
        }
75 3
    }
76
77
    /**
78
     * close all handlers
79
     */
80 4
    private function close()
81
    {
82 4
        foreach ($this->bag as $unit) {
83 4
            $unit->getFilesystem()->close();
84 4
        }
85 4
        $this->result->setActionEndTime($this->getCode(), new \DateTime());
86 4
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 5
    public function getCode()
92
    {
93 5
        return 'dump';
94
    }
95
}
96