Passed
Push — master ( d50d64...3e68ba )
by Maike
02:16
created

Connection::setAppLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MocOrm\Connection;
4
5
6
use phpDocumentor\Reflection\Types\Boolean;
7
8
class Connection
9
{
10
    /**
11
     * This instance object
12
     * @var Connection.
13
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment Connection. at position 0 could not be parsed: Unknown type name 'Connection.' at position 0 in Connection..
Loading history...
14
    private static $_instance;
15
16
    /**
17
     * This is instance current connect object connected
18
     * @var PDO
0 ignored issues
show
Bug introduced by
The type MocOrm\Connection\PDO was not found. Did you mean PDO? If so, make sure to prefix the type with \.
Loading history...
19
     */
20
    private $_connection;
21
22
    /**
23
     * @currentConnectionName string of the current connection name
24
     */
25
    private $_currentConnectionString;
26
27
    private $connectionString;
28
    private $driver;
29
    private $username;
30
    private $password;
31
    private $charset;
32
    private $schema;
33
    private $options;
34
35
    /**
36
     * This save all query orm use.
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...
37
     * @var query string
38
     */
39
    private $performed_query = [];
40
41
    /**
42
     * Initialize the object or return this object if have value set in attribute $_instance
43
     * @return Connection
44
     */
45
    public function __construct($config)
46
    {
47
        $this->connectionString = $config['connectionString'];
48
        $this->driver = $config['driver'];
49
        $this->username = $config['username'];
50
        $this->password = $config['password'];
51
        $this->charset = $config['charset'];
52
        $this->schema = $config['schema'];
53
54
        self::$_instance = $this;
55
        return $this;
56
    }
57
58
    /**
59
     * Initialize the connection
60
     * @param string $connectionName name on connection
61
     * @return $this This object from other interator
62
     * @throws \Exception if the connect name haven't set;
63
     */
64
    public function setConnection()
65
    {
66
        $this->_currentConnectionString = $this->connectionString;
67
68
        try {
69
            $this->_connection = new \PDO(
0 ignored issues
show
Documentation Bug introduced by
It seems like new PDO($this->connectio...rname, $this->password) of type PDO is incompatible with the declared type MocOrm\Connection\PDO of property $_connection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
                $this->connectionString,
71
                $this->username,
72
                $this->password
73
            );
74
75
            $charsetQuery = "set names '$this->charset'";
76
77
            $this->_connection->query($charsetQuery);
78
79
        } catch (\Exception $e) {
80
            throw new \Exception($e->getMessage(), $e->getCode());
81
        }
82
83
        return $this;
84
    }
85
86
    /**
87
     * Get name on current connection
88
     * @return string name on current connection or null if don't have
89
     */
90
    final public function getCurrentConnectionString()
91
    {
92
        return $this->_currentConnectionString;
93
    }
94
95
    /**
96
     * Initialize the connection
97
     * @return $this
98
     * @throws \Exception
99
     */
100
    public function connection($connectionString, $username, $password)
0 ignored issues
show
Unused Code introduced by
The parameter $connectionString is not used and could be removed. ( Ignorable by Annotation )

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

100
    public function connection(/** @scrutinizer ignore-unused */ $connectionString, $username, $password)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $password is not used and could be removed. ( Ignorable by Annotation )

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

100
    public function connection($connectionString, $username, /** @scrutinizer ignore-unused */ $password)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $username is not used and could be removed. ( Ignorable by Annotation )

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

100
    public function connection($connectionString, /** @scrutinizer ignore-unused */ $username, $password)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
    {
102
        if (is_null($this->getCurrentConnectionName())) throw new \Exception('Conexão não setada.');
0 ignored issues
show
Bug introduced by
The method getCurrentConnectionName() does not exist on MocOrm\Connection\Connection. Did you maybe mean getCurrentConnectionString()? ( Ignorable by Annotation )

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

102
        if (is_null($this->/** @scrutinizer ignore-call */ getCurrentConnectionName())) throw new \Exception('Conexão não setada.');

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...
103
104
        try {
105
            $connectionName = $this->getCurrentConnectionName();
106
107
            $this->_connection = new \PDO(
0 ignored issues
show
Documentation Bug introduced by
It seems like new PDO($this->_connecti...sword[$connectionName]) of type PDO is incompatible with the declared type MocOrm\Connection\PDO of property $_connection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
108
                $this->_connectionString[$connectionName],
0 ignored issues
show
Bug introduced by
The property _connectionString does not exist on MocOrm\Connection\Connection. Did you mean connectionString?
Loading history...
109
                $this->_username[$connectionName],
0 ignored issues
show
Bug introduced by
The property _username does not exist on MocOrm\Connection\Connection. Did you mean username?
Loading history...
110
                $this->_password[$connectionName]
0 ignored issues
show
Bug introduced by
The property _password does not exist on MocOrm\Connection\Connection. Did you mean password?
Loading history...
111
            );
112
113
        } catch (\Exception $e) {
114
            throw new \Exception($e->getMessage(), $e->getCode());
115
        }
116
117
        return $this;
118
    }
119
120
    public function getConnection()
121
    {
122
        return $this->_connection;
123
    }
124
125
    /**
126
     * @return query
127
     */
128
    public function getPerformedQuery()
129
    {
130
        return $this->performed_query;
131
    }
132
133
    /**
134
     * @return query
135
     */
136
    public function getLastPerformedQuery()
137
    {
138
        return end($this->performed_query);
0 ignored issues
show
Bug introduced by
$this->performed_query of type MocOrm\Connection\query is incompatible with the type array expected by parameter $array of end(). ( Ignorable by Annotation )

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

138
        return end(/** @scrutinizer ignore-type */ $this->performed_query);
Loading history...
139
    }
140
141
    /**
142
     * @param String $query
143
     * @param String $time
144
     * @return $this
145
     */
146
    public function setPerformedQuery(String $query, String $time)
147
    {
148
        $this->performed_query[] = ['query' => $query, 'time' => $time];
149
        return $this;
150
    }
151
152
    /**
153
     * @return array
154
     */
155
    public function getDriver()
156
    {
157
        return $this->driver;
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public function getSchema()
164
    {
165
        return $this->schema;
166
    }
167
168
    /**
169
     * Open transaction for insert, update, delete.
170
     * @return $this
171
     */
172
    final public function beginTransaction()
173
    {
174
        $this->_connection->beginTransaction();
175
        return $this;
176
    }
177
178
    /**
179
     * Using commit to all actions executed after begin transaction
180
     * @return $this
181
     */
182
    final public function commitTransaction()
183
    {
184
        $this->_connection->commit();
185
        return $this;
186
    }
187
188
    /**
189
     * Using rollback to all actions executed after begin transaction
190
     * @return $this
191
     */
192
    final public function rollbackTransaction()
193
    {
194
        $this->_connection->rollBack();
195
        return $this;
196
    }
197
198
    /**
199
     * Change schema on postgres
0 ignored issues
show
Bug introduced by
The type MocOrm\Connection\schema 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...
200
     * @param $schema schema name
201
     * @return $this
202
     */
203
    final public function changeSchema($schema = null)
204
    {
205
        if (!is_string($schema)) throw new \InvalidArgumentException('The parameter don\'t is an String.');
206
        if ($this->driver == 'mysql') throw new \InvalidArgumentException('This driver not supported schemas.');
207
208
        $this->getConnection()->exec("SET search_path TO '$schema';");
209
        return $this;
210
    }
211
212
    /**
213
     * @param bool $logger
214
     * @return Connection
215
     */
216
    final public function setAppLogger($logger): Connection {
217
        $this->options['appLogger'] = $logger;
218
219
        return $this;
220
    }
221
222
223
    final public function getAppLogger() {
224
        return $this->options['appLogger'];
225
    }
226
}
227