Passed
Push — master ( 46260f...cb5e2e )
by Jean Paul
01:23
created

DatabaseLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A insert() 0 17 4
A load() 0 3 1
1
<?php
2
3
namespace Coco\SourceWatcher\Core\Loaders;
4
5
use Coco\SourceWatcher\Core\Database\Connections\Connector;
6
use Coco\SourceWatcher\Core\IO\Outputs\DatabaseOutput;
7
use Coco\SourceWatcher\Core\Loader;
8
use Coco\SourceWatcher\Core\Row;
9
use Coco\SourceWatcher\Core\SourceWatcherException;
10
11
class DatabaseLoader extends Loader
12
{
13
    /**
14
     * @param Row $row
15
     * @throws SourceWatcherException
16
     */
17
    public function load ( Row $row )
18
    {
19
        $this->insert( $row );
20
    }
21
22
    /**
23
     * @param Row $row
24
     * @throws SourceWatcherException
25
     */
26
    protected function insert ( Row $row ) : void
27
    {
28
        if ( $this->output == null ) {
29
            throw new SourceWatcherException( "An output must be provided." );
30
        }
31
32
        $outputIsDatabaseOutput = $this->output instanceof DatabaseOutput;
33
34
        if ( !$outputIsDatabaseOutput ) {
35
            throw new SourceWatcherException( sprintf( "The output must be an instance of %s", DatabaseOutput::class ) );
36
        }
37
38
        if ( $this->output->getOutput() == null ) {
39
            throw new SourceWatcherException( "No connector found. Set a connector before trying to insert a row." );
40
        }
41
42
        $this->output->getOutput()->insert( $row );
43
    }
44
}
45