1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataDb\Export\Table; |
4
|
|
|
|
5
|
|
|
use Graze\DataDb\Export\Query\QueryExporter; |
6
|
|
|
use Graze\DataDb\QueryNode; |
7
|
|
|
use Graze\DataDb\TableNodeInterface; |
8
|
|
|
use Graze\DataFile\Format\FormatInterface; |
9
|
|
|
use Graze\DataFile\Helper\Builder\BuilderAwareInterface; |
10
|
|
|
use Graze\DataFile\Helper\Builder\BuilderTrait; |
11
|
|
|
use Graze\DataFile\Helper\OptionalLoggerTrait; |
12
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
13
|
|
|
use Psr\Log\LoggerAwareInterface; |
14
|
|
|
use Psr\Log\LogLevel; |
15
|
|
|
|
16
|
|
|
class TableExporter implements TableExporterInterface, BuilderAwareInterface, LoggerAwareInterface |
17
|
|
|
{ |
18
|
|
|
use OptionalLoggerTrait; |
19
|
|
|
use BuilderTrait; |
20
|
|
|
|
21
|
|
|
/** @var FileNodeInterface */ |
22
|
|
|
private $file; |
23
|
|
|
/** @var FormatInterface */ |
24
|
|
|
private $format; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* TableExporter constructor. |
28
|
|
|
* |
29
|
|
|
* @param FileNodeInterface $file |
30
|
|
|
* @param FormatInterface $format |
31
|
|
|
*/ |
32
|
|
|
public function __construct(FileNodeInterface $file, FormatInterface $format) |
33
|
|
|
{ |
34
|
|
|
$this->file = $file; |
35
|
|
|
$this->format = $format; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Export a table to something |
40
|
|
|
* |
41
|
|
|
* @param TableNodeInterface $table |
42
|
|
|
* |
43
|
|
|
* @return FileNodeInterface |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function export(TableNodeInterface $table) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
list ($sql, $bind) = $table->getAdapter()->getDialect()->getSelectSyntax($table); |
48
|
|
|
|
49
|
|
|
$query = $this->getBuilder()->build(QueryNode::class, $table->getAdapter(), $sql, $bind); |
50
|
|
|
|
51
|
|
|
$this->log(LogLevel::INFO, "Exporting table: {table} to file: {file}", [ |
52
|
|
|
'table' => $table, |
53
|
|
|
'file' => $this->file, |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
$exporter = $this->getBuilder()->build(QueryExporter::class, $this->file, $this->format); |
57
|
|
|
return $exporter->export($query); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.