Passed
Push — master ( 9ed26a...ca2be7 )
by Jean Paul
07:43
created

DatabaseExtractor::extract()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 19
rs 9.9666
1
<?php
2
3
namespace Coco\SourceWatcher\Core\Extractors;
4
5
use Coco\SourceWatcher\Core\Database\Connections\Connector;
6
use Coco\SourceWatcher\Core\Extractor;
7
use Coco\SourceWatcher\Core\Row;
8
use Coco\SourceWatcher\Core\SourceWatcherException;
9
10
/**
11
 * Class DatabaseExtractor
12
 *
13
 * @package Coco\SourceWatcher\Core\Extractors
14
 */
15
class DatabaseExtractor extends Extractor
16
{
17
    private ?Connector $databaseConnector = null;
18
19
    private ?string $query = null;
20
21
    public function __construct ( Connector $databaseConnector, string $query )
22
    {
23
        $this->databaseConnector = $databaseConnector;
24
        $this->query = $query;
25
    }
26
27
    /**
28
     * @return array
29
     * @throws SourceWatcherException
30
     */
31
    public function extract () : array
32
    {
33
        if ( $this->databaseConnector == null ) {
34
            throw new SourceWatcherException( "Database connector missing" );
35
        }
36
37
        if ( $this->query == null ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->query of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
38
            throw new SourceWatcherException( "Query missing" );
39
        }
40
41
        $result = [];
42
43
        $arrayResults = $this->databaseConnector->executePlainQuery( $this->query );
44
45
        foreach ( $arrayResults as $currentRecord ) {
46
            array_push( $result, new Row( $currentRecord ) );
47
        }
48
49
        return $result;
50
    }
51
}
52