Completed
Push — 2.0 ( 987f1a...8caec2 )
by Vermeulen
02:11
created

SqlConnect::useUtf8()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BfwSql;
4
5
use \Exception;
6
7
/**
8
 * Class to initialize a connection to a SQL server with PDO
9
 * 
10
 * @package bfw-sql
11
 * @author Vermeulen Maxime <[email protected]>
12
 * @version 2.0
13
 */
14
class SqlConnect
15
{
16
    /**
17
     * @const ERR_DSN_METHOD_NOT_FOUND Exception code if there is no method to
18
     * generate the dsn used by PDO.
19
     */
20
    const ERR_DSN_METHOD_NOT_FOUND = 2104001;
21
    
22
    /**
23
     * @var \PDO $PDO PDO Connexion object
24
     */
25
    protected $PDO;
26
    
27
    /**
28
     * @var \stdClass $connectionInfos All informations about the connection
29
     */
30
    protected $connectionInfos;
31
    
32
    /**
33
     * @var string $type Connexion type (mysql/pgsql/etc)
34
     */
35
    protected $type;
36
    
37
    /**
38
     * @var integer $nbQuery (default 0) Number of request has been done
39
     */
40
    protected $nbQuery = 0;
41
    
42
    /**
43
     * Constructor
44
     * Initialize the connection
45
     * 
46
     * @param object $connectionInfos All informations about the connection
47
     * 
48
     * @throw \PDOException If Connexion fail
49
     */
50
    public function __construct($connectionInfos)
51
    {
52
        $this->connectionInfos = $connectionInfos;
53
        $this->type            = &$this->connectionInfos->baseType;
54
    }
55
    
56
    /**
57
     * Initialize the connection
58
     * 
59
     * @throw \PDOException If Connexion fail
60
     * 
61
     * @return void
62
     */
63
    public function createConnection()
64
    {
65
        $usedClass          = \BfwSql\UsedClass::getInstance();
66
        $createDsnClassName = $usedClass->obtainClassNameToUse('CreatePdoDsn');
67
        $pdoClassName       = $usedClass->obtainClassNameToUse('PDO');
68
        
69
        if (!method_exists($createDsnClassName, $this->type)) {
70
            throw new Exception(
71
                'No method to generate DSN find on \BfwSql\CreatePdoDsn class.',
72
                self::ERR_DSN_METHOD_NOT_FOUND
73
            );
74
        }
75
        
76
        $this->PDO = new $pdoClassName(
77
            $createDsnClassName::{$this->type}($this->connectionInfos),
78
            $this->connectionInfos->user,
79
            $this->connectionInfos->password,
80
            $this->connectionInfos->pdoOptions
81
        );
82
            
83
        $this->pdoSetAttributes();
84
            
85
        if ($this->connectionInfos->mysqlUtf8 === true) {
86
            $this->mysqlUtf8();
87
        }
88
    }
89
    
90
    /**
91
     * Define attributes for PDO.
92
     * List of attributes is define in base config on key pdoAttributes
93
     * 
94
     * @link http://php.net/manual/en/pdo.setattribute.php
95
     * 
96
     * @return void
97
     */
98
    protected function pdoSetAttributes()
99
    {
100
        $pdoAttributes = $this->connectionInfos->pdoAttributes;
101
        
102
        foreach ($pdoAttributes as $attributeName => $attributeValue) {
103
            $this->PDO->setAttribute($attributeName, $attributeValue);
104
        }
105
    }
106
    
107
    /**
108
     * Define charset to UTF-8 with mysql
109
     * 
110
     * @return void
111
     */
112
    protected function mysqlUtf8()
113
    {
114
        $this->PDO->exec('SET NAMES utf8');
115
    }
116
    
117
    /**
118
     * Protect a data with the system implemented by the pdo drivers used.
119
     * 
120
     * @param string $datas Data to protect
121
     * 
122
     * @return string
123
     */
124
    public function protect(string $datas): string
125
    {
126
        $protectedString = $this->PDO->quote($datas);
127
        
128
        /**
129
         * The quote method add the caracter ' on the start and the end of the
130
         * protected string.
131
         * So we remove this quote at the start and the end of the string.
132
         */
133
        return substr($protectedString, 1, strlen($protectedString) - 2);
134
    }
135
    
136
    /**
137
     * Getter to access at the property PDO
138
     * 
139
     * @return \PDO
140
     */
141
    public function getPDO()
142
    {
143
        return $this->PDO;
144
    }
145
    
146
    /**
147
     * Getter to access at the property connectionInfos
148
     * 
149
     * @return object
150
     */
151
    public function getConnectionInfos()
152
    {
153
        return $this->connectionInfos;
154
    }
155
    
156
    /**
157
     * Getter to access at the property type
158
     * 
159
     * @return string
160
     */
161
    public function getType() : string
162
    {
163
        return $this->type;
164
    }
165
    
166
    /**
167
     * Getter to access at the property nbQuery
168
     * 
169
     * @return integer
170
     */
171
    public function getNbQuery(): int
172
    {
173
        return $this->nbQuery;
174
    }
175
    
176
    /**
177
     * Increment the number of query has been done
178
     * 
179
     * @return void
180
     */
181
    public function upNbQuery()
182
    {
183
        $this->nbQuery++;
184
    }
185
}
186