Passed
Push — master ( 0d599d...f724be )
by Jean Paul
01:43
created

DatabaseLoader::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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",
38
                DatabaseOutput::class ) );
39
        }
40
41
        $output = $this->output->getOutput();
42
43
        if ( $output == null || empty( $output ) || ( sizeof( $output ) == 1 && $output[0] == null ) ) {
44
            throw new SourceWatcherException( "No database connector found. Set a connector before trying to insert a row" );
45
        }
46
47
        foreach ( $output as $currentConnector ) {
48
            if ( $currentConnector != null ) {
49
                $currentConnector->insert( $row );
50
            }
51
        }
52
    }
53
54
    public function getArrayRepresentation () : array
55
    {
56
        $result = parent::getArrayRepresentation();
57
58
        $result["output"] = [];
59
60
        $output = $this->output->getOutput();
0 ignored issues
show
Bug introduced by
The method getOutput() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        /** @scrutinizer ignore-call */ 
61
        $output = $this->output->getOutput();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
62
        foreach ( $output as $currentConnector ) {
63
            if ( $currentConnector != null ) {
64
                $result["output"][] = [
65
                    "class" => get_class( $currentConnector ),
66
                    "parameters" => $currentConnector->getConnectionParameters()
67
                ];
68
            }
69
        }
70
71
        return $result;
72
    }
73
}
74