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

EmbeddedDatabaseConnector::insert()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 20
rs 9.9332
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