Passed
Push — master ( 9ed26a...ca2be7 )
by Jean Paul
07:43
created

Connector::executePlainQuery()   A

Complexity

Conditions 4
Paths 16

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 16
nop 1
dl 0
loc 16
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\Internationalization;
8
use Doctrine\DBAL\Connection;
9
use Doctrine\DBAL\DriverManager;
10
use Exception;
11
12
/**
13
 * Class Connector
14
 *
15
 * @package Coco\SourceWatcher\Core\Database\Connections
16
 */
17
abstract class Connector
18
{
19
    protected string $driver = "";
20
21
    protected array $connectionParameters = [];
22
23
    protected string $user = "";
24
25
    protected string $password = "";
26
27
    protected string $tableName = "";
28
29
    public function getDriver () : string
30
    {
31
        return $this->driver;
32
    }
33
34
    protected abstract function getConnectionParameters () : array;
35
36
    public function getUser () : string
37
    {
38
        return $this->user;
39
    }
40
41
    public function setUser ( string $user ) : void
42
    {
43
        $this->user = $user;
44
    }
45
46
    public function getPassword () : string
47
    {
48
        return $this->password;
49
    }
50
51
    public function setPassword ( string $password ) : void
52
    {
53
        $this->password = $password;
54
    }
55
56
    public function getTableName () : string
57
    {
58
        return $this->tableName;
59
    }
60
61
    public function setTableName ( string $tableName ) : void
62
    {
63
        $this->tableName = $tableName;
64
    }
65
66
    /**
67
     * @return Connection
68
     * @throws Exception
69
     */
70
    public function getConnection () : Connection
71
    {
72
        return DriverManager::getConnection( $this->getConnectionParameters() );
73
    }
74
75
    /**
76
     * @param Row $row
77
     * @return int
78
     * @throws SourceWatcherException
79
     */
80
    public function insert ( Row $row ) : int
81
    {
82
        if ( $this->tableName == null || $this->tableName == "" ) {
83
            throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class,
84
                "No_Table_Name_Found" ) );
85
        }
86
87
        try {
88
            $connection = $this->getConnection();
89
90
            if ( !$connection->isConnected() ) {
91
                $connection->connect();
92
            }
93
        } catch ( Exception $exception ) {
94
            throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class,
95
                "Connection_Object_Not_Connected_Cannot_Insert" ), 0, $exception );
96
        }
97
98
        try {
99
            $numberOfAffectedRows = $connection->insert( $this->tableName, $row->getAttributes() );
100
101
            $connection->close();
102
        } catch ( Exception $exception ) {
103
            throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class,
104
                "Unexpected_Error" ), 0, $exception );
105
        }
106
107
        return $numberOfAffectedRows;
108
    }
109
110
    /**
111
     * @param string $query
112
     * @return array
113
     * @throws SourceWatcherException
114
     */
115
    public function executePlainQuery ( string $query ) : array
116
    {
117
        try {
118
            $connection = $this->getConnection();
119
120
            if ( !$connection->isConnected() ) {
121
                $connection->connect();
122
            }
123
124
            $statement = $connection->executeQuery( $query );
125
126
            return $statement->fetchAllAssociative();
0 ignored issues
show
Bug introduced by
The method fetchAllAssociative() does not exist on Doctrine\DBAL\Driver\Statement. Did you maybe mean fetchAll()? ( Ignorable by Annotation )

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

126
            return $statement->/** @scrutinizer ignore-call */ fetchAllAssociative();

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...
127
        } catch ( \Doctrine\DBAL\Driver\Exception $e ) {
128
            throw new SourceWatcherException( "Something went wrong: " . $e->getMessage(), 0, $e );
129
        } catch ( Exception $e ) {
130
            throw new SourceWatcherException( "Something unexpected went wrong: " . $e->getMessage(), 0, $e );
131
        }
132
    }
133
}
134