RedshiftFileImporter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B import() 0 29 6
1
<?php
2
3
namespace Graze\DataDb\Import;
4
5
use Graze\DataDb\Dialect\RedshiftDialect;
6
use Graze\DataDb\Helper\RedshiftHelper;
7
use Graze\DataDb\TableNodeInterface;
8
use Graze\DataFile\Format\CsvFormatInterface;
9
use Graze\DataFile\Format\FormatAwareInterface;
10
use Graze\DataFile\Format\JsonFormatInterface;
11
use Graze\DataFile\Helper\Builder\BuilderAwareInterface;
12
use Graze\DataFile\Helper\Builder\BuilderTrait;
13
use Graze\DataFile\Node\FileNodeInterface;
14
use InvalidArgumentException;
15
use League\Flysystem\AwsS3v3\AwsS3Adapter;
16
17
class RedshiftFileImporter implements FileImporterInterface, BuilderAwareInterface
18
{
19
    use BuilderTrait;
20
21
    /** @var TableNodeInterface */
22
    private $table;
23
    /** @var RedshiftDialect */
24
    private $dialect;
25
26
    /**
27
     * RedshiftFileImporter constructor.
28
     *
29
     * @param TableNodeInterface $table
30
     */
31
    public function __construct(TableNodeInterface $table)
32
    {
33
        $this->dialect = $table->getAdapter()->getDialect();
34
        if (!$this->dialect instanceof RedshiftDialect) {
35
            throw new InvalidArgumentException("The provided table: $table is not a redshift table");
36
        }
37
        $this->table = $table;
38
    }
39
40
    /**
41
     * @param FileNodeInterface $file
42
     *
43
     * @return TableNodeInterface
44
     */
45
    public function import(FileNodeInterface $file)
46
    {
47
        $helper = $this->getBuilder()->build(RedshiftHelper::class, $this->dialect);
48
49
        if (!$file->getFilesystem()->getAdapter() instanceof AwsS3Adapter) {
50
            throw new InvalidArgumentException("The supplied file: $file is required to be in S3 for import into Redshift");
51
        }
52
53
        if ($file instanceof FormatAwareInterface) {
54
            $format = $file->getFormat();
55
            if (!$helper->isValidImportFormat($format)) {
56
                throw new InvalidArgumentException("The supplied file: $file does not have a valid format for redshift");
57
            }
58
        } else {
59
            throw new InvalidArgumentException("No formatting could not be determined from the supplied file: $file");
60
        }
61
62
        if ($format instanceof JsonFormatInterface) {
63
            list($sql, $bind) = $this->dialect->getImportFromJson($this->table, $file, $format);
0 ignored issues
show
Documentation introduced by
$format is of type object<Graze\DataFile\Format\JsonFormatInterface>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
        } elseif ($format instanceof CsvFormatInterface) {
65
            list($sql, $bind) = $this->dialect->getImportFromCsv($this->table, $file, $format);
66
        } else {
67
            throw new InvalidArgumentException("The format type: " . get_class($format) . " can not be used to import into redshift");
68
        }
69
70
        $this->table->getAdapter()->query($sql, $bind);
71
72
        return $this->table;
73
    }
74
}
75