Completed
Push — master ( 797c9e...b671da )
by Matteo
02:15
created

ManageConnections::connect()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 6
nc 8
nop 5
1
<?php
2
3
namespace Mattbit\MysqlCompat\BridgeComponents;
4
5
use PDO;
6
use Mattbit\MysqlCompat\MysqlConstants;
7
use Mattbit\MysqlCompat\Exception\NotSupportedException;
8
9
trait ManageConnections
10
{
11
    public function clientEncoding(Connection $linkIdentifier = null)
12
    {
13
        $connection = $this->manager->getOpenConnectionOrFail($linkIdentifier);
1 ignored issue
show
Bug introduced by
The property manager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
15
        return $connection->getCharset();
16
    }
17
18
    public function close(Connection $linkIdentifier = null)
19
    {
20
        return $this->manager->disconnect($linkIdentifier);
21
    }
22
23
    public function connect($server = null, $username = null, $password = null, $newLink = false, $clientFlags = 0)
24
    {
25
        if ($server   === null) $server   = ini_get("mysql.default_host");
26
        if ($username === null) $username = ini_get("mysql.default_user");
27
        if ($password === null) $password = ini_get("mysql.default_password");
28
29
        $options = $this->parseClientFlags($clientFlags);
30
31
        return $this->manager->connect("mysql:host={$server};", $username, $password, $options, $newLink);
32
    }
33
34
    protected function parseClientFlags($clientFlags)
35
    {
36
        $options = [];
37
38
        if ($clientFlags & MysqlConstants::CLIENT_COMPRESS) {
39
            $options[PDO::MYSQL_ATTR_COMPRESS] = 1;
40
        }
41
42
        if ($clientFlags & MysqlConstants::CLIENT_IGNORE_SPACE) {
43
            $options[PDO::MYSQL_ATTR_IGNORE_SPACE] = 1;
44
        }
45
46
        if ($clientFlags & MysqlConstants::CLIENT_SSL) {
47
            throw new NotSupportedException("SSL is not supported. You must create the PDO instance manually.");
48
        }
49
50
        if ($clientFlags & MysqlConstants::CLIENT_INTERACTIVE) {
51
            throw new NotSupportedException("Interactive client is not supported by PDO.");
52
        }
53
54
        return $options;
55
    }
56
57
58
    public function pconnect()
59
    {
60
        // @todo
61
    }
62
}
63