Passed
Push — master ( 2a0eac...eecbbb )
by Maike
01:58
created

Connection::connection()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 1
nop 3
dl 0
loc 18
ccs 0
cts 14
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Connection::getLastPerformedQuery() 0 3 1
1
<?php
2
3
namespace MocOrm\Connection;
4
5
class Connection
6
{
7
    /**
8
     * This instance object
9
     * @var Connection \Connection
10
     */
11
    private static $_instance;
12
13
    /**
14
     * This is instance current connect object connected
15
     * @var \PDO $_connection
16
     */
17
    private $_connection;
18
19
    /**
20
     * @currentConnectionName string of the current connection name
21
     */
22
    private $_currentConnectionString;
23
24
    private $connectionString;
25
    private $driver;
26
    private $username;
27
    private $password;
28
    private $charset;
29
    private $schema;
30
    private $options;
31
32
    /**
33
     * This save all query orm use.
34
     * @var Array Query strings
35
     */
36
    private $performed_query = [];
37
38
    /**
39
     * Initialize the object or return this object if have value set in attribute $_instance
40
     * @return Connection
41
     */
42
    public function __construct($config)
43
    {
44
        $this->connectionString = $config['connectionString'];
45
        $this->driver = $config['driver'];
46
        $this->username = $config['username'];
47
        $this->password = $config['password'];
48
        $this->charset = $config['charset'];
49
        $this->schema = $config['schema'];
50
51
        self::$_instance = $this;
52
        return $this;
53
    }
54
55
    /**
56
     * Initialize the connection
57
     * @param string $connectionName name on connection
58
     * @return $this This object from other interator
59
     * @throws \Exception if the connect name haven't set;
60
     */
61
    public function setConnection()
62
    {
63
        $this->_currentConnectionString = $this->connectionString;
64
65
        try {
66
            $this->_connection = new \PDO(
67
                $this->connectionString,
68
                $this->username,
69
                $this->password
70
            );
71
72
            $charsetQuery = "set names '$this->charset'";
73
74
            $this->_connection->query($charsetQuery);
75
76
        } catch (\Exception $e) {
77
            throw new \Exception($e->getMessage(), $e->getCode());
78
        }
79
80
        return $this;
81
    }
82
83
    /**
84
     * Get name on current connection
85
     * @return string name on current connection or null if don't have
86
     */
87
    final public function getCurrentConnectionString()
88
    {
89
        return $this->_currentConnectionString;
90
    }
91
92
    public function getConnection()
93
    {
94
        return $this->_connection;
95
    }
96
97
    /**
98
     * @return query
0 ignored issues
show
Bug introduced by
The type MocOrm\Connection\query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
99
     */
100
    public function getPerformedQuery()
101
    {
102
        return $this->performed_query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->performed_query returns the type array which is incompatible with the documented return type MocOrm\Connection\query.
Loading history...
103
    }
104
105
    /**
106
     * @return String query
107
     */
108
    public function getLastPerformedQuery()
109
    {
110
        return end($this->performed_query);
111
    }
112
113
    /**
114
     * @param String $query
115
     * @param String $time
116
     * @return $this
117
     */
118
    public function setPerformedQuery(String $query, String $time)
119
    {
120
        $this->performed_query[] = ['query' => $query, 'time' => $time];
121
122
        return $this;
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function getDriver()
129
    {
130
        return $this->driver;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getSchema()
137
    {
138
        return $this->schema;
139
    }
140
141
    /**
142
     * Open transaction for insert, update, delete.
143
     * @return $this
144
     */
145
    final public function beginTransaction()
146
    {
147
        $this->_connection->beginTransaction();
148
        return $this;
149
    }
150
151
    /**
152
     * Using commit to all actions executed after begin transaction
153
     * @return $this
154
     */
155
    final public function commitTransaction()
156
    {
157
        $this->_connection->commit();
158
        return $this;
159
    }
160
161
    /**
162
     * Using rollback to all actions executed after begin transaction
163
     * @return $this
164
     */
165
    final public function rollbackTransaction()
166
    {
167
        $this->_connection->rollBack();
168
        return $this;
169
    }
170
171
    /**
172
     * Change schema on postgres
173
     * @param String $schema schema name
174
     * @return $this
175
     */
176
    final public function changeSchema($schema = null)
177
    {
178
        if (!is_string($schema)) throw new \InvalidArgumentException('The parameter don\'t is an String.');
179
        if ($this->driver == 'mysql') throw new \InvalidArgumentException('This driver not supported schemas.');
180
181
        $this->getConnection()->exec("SET search_path TO '$schema';");
182
        return $this;
183
    }
184
185
    /**
186
     * @param bool $logger
187
     * @return Connection
188
     */
189
    final public function setAppLogger($logger): Connection {
190
        $this->options['appLogger'] = $logger;
191
192
        return $this;
193
    }
194
195
196
    final public function getAppLogger() {
197
        return $this->options['appLogger'];
198
    }
199
}
200