Passed
Pull Request — master (#38)
by Teye
05:43
created

SqlInputSource::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.8666
cc 2
nc 2
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\InputSources;
5
6
class SqlInputSource implements InputSourceInterface
7
{
8
    /**
9
     * @var string[]
10
     */
11
    protected array $sqls;
12
13
    protected bool $foldCase;
14
15
    protected string $connectURI;
16
17
    protected string $username;
18
19
    protected string $password;
20
21
    /**
22
     * SqlInputSource constructor.
23
     *
24
     * @param string   $connectURI
25
     * @param string   $username
26
     * @param string   $password
27
     * @param string[] $sqls     List of SQL queries where each SQL query would retrieve the data to be indexed.
28
     * @param bool     $foldCase Toggle case folding of database column names. This may be enabled in cases where the
29
     *                           database returns case insensitive column names in query results.
30
     */
31 2
    public function __construct(
32
        string $connectURI,
33
        string $username,
34
        string $password,
35
        array $sqls,
36
        bool $foldCase = false
37
    ) {
38 2
        $this->sqls       = $sqls;
39 2
        $this->foldCase   = $foldCase;
40 2
        $this->connectURI = $connectURI;
41 2
        $this->username   = $username;
42 2
        $this->password   = $password;
43
    }
44
45
    /**
46
     * @return array<string,string|true|array<string,string|string[]>>
47
     */
48 2
    public function toArray(): array
49
    {
50 2
        $response = [
51
            'type'     => 'sql',
52
            'database' => [
53 2
                'type'            => $this->databaseTypeFromConnectUri($this->connectURI),
54
                'connectorConfig' => [
55 2
                    'connectURI' => $this->connectURI,
56 2
                    'user'       => $this->username,
57 2
                    'password'   => $this->password,
58
                ],
59
            ],
60 2
            'sqls'     => $this->sqls,
61
        ];
62
63 2
        if ($this->foldCase) {
64 1
            $response['foldCase'] = $this->foldCase;
65
        }
66
67 2
        return $response;
68
    }
69
70 2
    protected function databaseTypeFromConnectUri(string $connectURI): string
71
    {
72 2
        return (strpos(strtolower($connectURI), 'postgresql') !== false) ? 'postgresql' : 'mysql';
73
    }
74
}