RedshiftHelper::isValidExportFormat()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 7.7777
cc 8
eloc 9
nc 8
nop 1
1
<?php
2
3
namespace Graze\DataDb\Helper;
4
5
use Graze\DataDb\Dialect\DialectInterface;
6
use Graze\DataDb\Dialect\RedshiftDialect;
7
use Graze\DataDb\TableNodeInterface;
8
use Graze\DataFile\Format\CsvFormat;
9
use Graze\DataFile\Format\CsvFormatInterface;
10
use Graze\DataFile\Format\FormatInterface;
11
use Graze\DataFile\Format\JsonFormat;
12
use Graze\DataFile\Format\JsonFormatInterface;
13
use Psr\Log\LogLevel;
14
15
class RedshiftHelper extends AbstractHelper
16
{
17
    /**
18
     * RedshiftDialect constructor.
19
     *
20
     * @param DialectInterface|null $dialect
21
     * @param string|null           $timezone
22
     */
23
    public function __construct(DialectInterface $dialect = null, $timezone = null)
24
    {
25
        $this->dialect = $dialect ?: new RedshiftDialect(null, $timezone);
26
    }
27
28
    /**
29
     * @param TableNodeInterface $table
30
     * @param array              $columns [[:column, :type, :nullable, ::primary, :index]]
31
     *
32
     * @return bool
33
     */
34 View Code Duplication
    public function createTable(TableNodeInterface $table, array $columns)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
35
    {
36
        $this->log(LogLevel::INFO, "Creating Table {table} with columns: {columns}", [
37
            'table'   => $table->getFullName(),
38
            'columns' => implode(',', array_keys($columns)),
39
        ]);
40
41
        $dist = [];
42
        $sorted = [];
43
        $columnStrings = [];
44
45
        foreach ($columns as $column) {
46
            if ($column['primary'] && !$dist) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dist of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
47
                $dist[] = $this->dialect->getPrimaryKeyDefinition($column);
48
            } elseif ($column['primary'] || $column['index']) {
49
                $sorted[] = $this->dialect->getIndexDefinition($column);
50
            }
51
52
            $columnStrings[] = $this->dialect->getColumnDefinition($column);
53
        }
54
55
        list ($sql, $params) = $this->dialect->getCreateTable($table, $columnStrings, $dist, $sorted);
56
57
        $db = $table->getAdapter();
58
        $db->query(trim($sql), $params);
59
60
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type declared by the interface Graze\DataDb\Helper\HelperInterface::createTable of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
61
    }
62
63
    /**
64
     * @param TableNodeInterface $table
65
     *
66
     * @return array [:column => [:schema, :table, :column, :type, :nullable, :primary, :index]]
67
     */
68
    public function describeTable(TableNodeInterface $table)
69
    {
70
        list ($sql, $params) = $this->dialect->getDescribeTable($table);
71
72
        $db = $table->getAdapter();
73
        $description = $db->fetchAll(trim($sql), $params);
74
75
        $output = [];
76
77
        foreach ($description as $row) {
78
            $output[$row['column']] = [
79
                'schema'   => $table->getSchema(),
80
                'table'    => $table->getTable(),
81
                'column'   => $row['column'],
82
                'type'     => $row['type'],
83
                'nullable' => (bool) $row['notnull'],
84
                'primary'  => (bool) $row['distkey'],
85
                'index'    => (bool) ($row['sortkey'] != 0),
86
            ];
87
        }
88
89
        return $output;
90
    }
91
92
    /**
93
     * Produce the create syntax for a table
94
     *
95
     * @param TableNodeInterface $table
96
     *
97
     * @return string
98
     */
99
    public function getCreateSyntax(TableNodeInterface $table)
100
    {
101
        list ($sql, $params) = $this->dialect->getCreateSyntax($table);
102
103
        $db = $table->getAdapter();
104
        $result = $db->fetchAll(trim($sql), $params);
105
106
        if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
107
            $col = array_map(function ($row) {
108
                return $row['ddl'];
109
            }, $result);
110
111
            return implode("\n", $col);
112
        } else {
113
            return null;
114
        }
115
    }
116
117
    /**
118
     * @return FormatInterface
119
     */
120 View Code Duplication
    public function getDefaultExportFormat()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
121
    {
122
        return new CsvFormat([
123
            CsvFormat::OPTION_DELIMITER    => ',',
124
            CsvFormat::OPTION_NEW_LINE     => "\n",
125
            CsvFormat::OPTION_QUOTE        => '"',
126
            CsvFormat::OPTION_NULL         => '\\N',
127
            CsvFormat::OPTION_HEADER_ROW   => -1,
128
            CsvFormat::OPTION_ESCAPE       => '\\',
129
            CsvFormat::OPTION_ENCODING     => 'UTF-8',
130
            CsvFormat::OPTION_DOUBLE_QUOTE => false,
131
            CsvFormat::OPTION_BOM          => null,
132
        ]);
133
    }
134
135
    /**
136
     * @return FormatInterface
137
     */
138
    public function getDefaultImportFormat()
139
    {
140
        return new JsonFormat([
141
            JsonFormat::OPTION_FILE_TYPE => JsonFormat::JSON_FILE_TYPE_EACH_LINE,
142
        ]);
143
    }
144
145
    /**
146
     * @param FormatInterface $format
147
     *
148
     * @return bool
149
     */
150
    public function isValidExportFormat(FormatInterface $format)
151
    {
152
        return ($format->getType() == 'csv'
153
            && $format instanceof CsvFormatInterface
154
            && $format->getNewLine() == "\n"
155
            && $format->getQuote() == '"'
156
            && $format->getEscape() == '\\'
157
            && $format->getEncoding() == 'UTF-8'
158
            && !$format->useDoubleQuotes()
159
            && is_null($format->getBom()));
160
    }
161
162
    /**
163
     * @param FormatInterface $format
164
     *
165
     * @return bool
166
     */
167
    public function isValidImportFormat(FormatInterface $format)
168
    {
169
        return (
170
            ($format->getType() == 'csv'
171
                && $format instanceof CsvFormatInterface
172
                && $format->getNewLine() == "\n"
173
                && $format->getQuote() == '"'
174
                && (
175
                    ($format->getEscape() == '\\' && !$format->useDoubleQuotes()) ||
176
                    (!$format->hasEscape() && $format->useDoubleQuotes())
177
                )
178
                && $format->getEncoding() == 'UTF-8'
179
                && is_null($format->getBom()))
180
            || ($format->getType() == 'json'
181
                && $format instanceof JsonFormatInterface
182
                && $format->getJsonFileType() == JsonFormat::JSON_FILE_TYPE_EACH_LINE
183
            )
184
        );
185
    }
186
}
187