RedshiftQueryExporter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 57
c 0
b 0
f 0
wmc 5
lcom 1
cbo 8
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A validateFile() 0 6 1
A export() 0 16 2
1
<?php
2
3
namespace Graze\DataDb\Export\Query;
4
5
use Graze\DataDb\Helper\RedshiftHelper;
6
use Graze\DataDb\QueryNodeInterface;
7
use Graze\DataFile\Format\FormatAwareInterface;
8
use Graze\DataFile\Helper\Builder\BuilderAwareInterface;
9
use Graze\DataFile\Helper\Builder\BuilderTrait;
10
use Graze\DataFile\Helper\OptionalLoggerTrait;
11
use Graze\DataFile\Node\FileNodeInterface;
12
use InvalidArgumentException;
13
use League\Flysystem\AwsS3v3\AwsS3Adapter;
14
use Psr\Log\LoggerAwareInterface;
15
use Psr\Log\LogLevel;
16
17
class RedshiftQueryExporter implements QueryExporterInterface, BuilderAwareInterface, LoggerAwareInterface
18
{
19
    use OptionalLoggerTrait;
20
    use BuilderTrait;
21
22
    /** @var FileNodeInterface */
23
    private $file;
24
25
    /**
26
     * RedshiftQueryExporter constructor.
27
     *
28
     * @param FileNodeInterface $file
29
     */
30
    public function __construct(FileNodeInterface $file)
31
    {
32
        if (!$this->validateFile($file)) {
33
            throw new InvalidArgumentException("The supplied file: $file should be a s3 file");
34
        }
35
        $this->file = $file;
36
    }
37
38
    /**
39
     * @param FileNodeInterface $file
40
     *
41
     * @return bool
42
     */
43
    private function validateFile(FileNodeInterface $file)
44
    {
45
        $filesystem = $file->getFilesystem();
46
        $adapter = $filesystem->getAdapter();
47
        return ($adapter instanceof AwsS3Adapter);
48
    }
49
50
    /**
51
     * Export a table to somethingMysqlTableExporter
52
     *
53
     * @param QueryNodeInterface $query
54
     *
55
     * @return FileNodeInterface
56
     */
57
    public function export(QueryNodeInterface $query)
58
    {
59
        $helper = $this->getBuilder()->build(RedshiftHelper::class, $query->getAdapter()->getDialect());
60
        $format = $helper->getDefaultExportFormat();
61
62
        $this->log(LogLevel::INFO, "Exporting redshift query to file: {file}", ['file' => $this->file]);
63
        /** @var RedshiftExportQuery $exportQuery */
64
        $exportQuery = $this->getBuilder()->build(RedshiftExportQuery::class, $query, $this->file, $format);
65
66
        $exportQuery->query();
67
68
        if ($this->file instanceof FormatAwareInterface) {
69
            $this->file->setFormat($format);
70
        }
71
        return $this->file;
72
    }
73
}
74