Passed
Push — master ( 72d254...356a09 )
by Jean Paul
01:39
created

EmbeddedDatabaseConnector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A insert() 0 20 4
1
<?php
2
3
namespace Coco\SourceWatcher\Core\Database\Connections;
4
5
use Coco\SourceWatcher\Core\Row;
6
use Coco\SourceWatcher\Core\SourceWatcherException;
7
use Coco\SourceWatcher\Utils\i18n;
8
use Doctrine\DBAL\DBALException;
9
10
/**
11
 * Class EmbeddedDatabaseConnector
12
 * @package Coco\SourceWatcher\Core\Database\Connections
13
 */
14
abstract class EmbeddedDatabaseConnector extends Connector
15
{
16
    /**
17
     * @param Row $row
18
     * @return int
19
     * @throws SourceWatcherException
20
     */
21
    public function insert ( Row $row ) : int
22
    {
23
        if ( $this->tableName == null || $this->tableName == "" ) {
24
            throw new SourceWatcherException( i18n::getInstance()->getText( EmbeddedDatabaseConnector::class, "No_Table_Name_Found" ) );
25
        }
26
27
        $connection = $this->connect();
28
29
        // For some reason, for embedded databases such as SQLite, the DBAL Connection object will return false when asked if isConnected.
30
31
        try {
32
            $numberOfAffectedRows = $connection->insert( $this->tableName, $row->getAttributes() );
33
34
            $connection->close();
35
        } catch ( DBALException $dbalException ) {
36
            $errorMessage = sprintf( "Something went wrong while trying to insert the row: %s", $dbalException->getMessage() );
37
            throw new SourceWatcherException( $errorMessage );
38
        }
39
40
        return $numberOfAffectedRows;
41
    }
42
}
43