Passed
Push — master ( a535f0...f38dcf )
by Jean Paul
08:30
created

DatabaseLoader::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Coco\SourceWatcher\Core\Loaders;
4
5
use Coco\SourceWatcher\Core\IO\Outputs\DatabaseOutput;
6
use Coco\SourceWatcher\Core\Loader;
7
use Coco\SourceWatcher\Core\Row;
8
use Coco\SourceWatcher\Core\SourceWatcherException;
9
10
/**
11
 * Class DatabaseLoader
12
 *
13
 * @package Coco\SourceWatcher\Core\Loaders
14
 */
15
class DatabaseLoader extends Loader
16
{
17
    /**
18
     * @param Row $row
19
     * @throws SourceWatcherException
20
     */
21
    public function load ( Row $row )
22
    {
23
        $this->insert( $row );
24
    }
25
26
    /**
27
     * @param Row $row
28
     * @throws SourceWatcherException
29
     */
30
    protected function insert ( Row $row ) : void
31
    {
32
        if ( $this->output == null ) {
33
            throw new SourceWatcherException( "An output must be provided" );
34
        }
35
36
        if ( !( $this->output instanceof DatabaseOutput ) ) {
37
            throw new SourceWatcherException( sprintf( "The output must be an instance of %s", DatabaseOutput::class ) );
38
        }
39
40
        if ( $this->output->getOutput() == null ) {
41
            throw new SourceWatcherException( "No database connector found. Set a connector before trying to insert a row" );
42
        }
43
44
        $this->output->getOutput()->insert( $row );
45
    }
46
47
    public function getArrayRepresentation () : array
48
    {
49
        $result = parent::getArrayRepresentation();
50
51
        $dbOutput = $this->getOutput();
52
        $dbConnector = $dbOutput->getOutput();
53
54
        $result["output"] = [
55
            "class" => get_class( $dbConnector ),
56
            "parameters" => $dbConnector->getConnectionParameters()
57
        ];
58
59
        return $result;
60
    }
61
}
62