Completed
Push — master ( dd22c5...37f54e )
by Hong
02:16
created

Driver::setDefaultAttributes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Db
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Db\Driver\Pdo;
16
17
use Phossa2\Db\Message\Message;
18
use Phossa2\Db\Driver\DriverAbstract;
19
use Phossa2\Db\Exception\LogicException;
20
use Phossa2\Db\Interfaces\StatementInterface;
21
22
/**
23
 * Driver
24
 *
25
 * PDO driver
26
 *
27
 * @package Phossa2\Db
28
 * @author  Hong Zhang <[email protected]>
29
 * @see     DriverAbstract
30
 * @version 2.0.0
31
 * @since   2.0.0 added
32
 */
33
class Driver extends DriverAbstract
34
{
35
    /**
36
     * the connection link
37
     *
38
     * @var    \PDO
39
     * @access protected
40
     */
41
    protected $link;
42
43
    /**
44
     * Default PDO attributes
45
     *
46
     * @var    array
47
     * @access protected
48
     */
49
    protected $attributes = [
50
        'PDO::ATTR_ERRMODE'             => \PDO::ERRMODE_SILENT,
51
        'PDO::ATTR_CASE'                => \PDO::CASE_NATURAL,
52
        'PDO::ATTR_ORACLE_NULLS'        => \PDO::NULL_NATURAL,
53
        'PDO::ATTR_DEFAULT_FETCH_MODE'  => \PDO::FETCH_ASSOC,
54
        'PDO::ATTR_EMULATE_PREPARES'    => false,
55
    ];
56
57
    /**
58
     * Driver constructor
59
     *
60
     * @param  array $connectInfo
61
     * @param  StatementInterface $statementPrototype
62
     * @throws InvalidArgumentException if link type not right
63
     * @throws LogicException driver specific extension not loaded
64
     * @access public
65
     */
66
    public function __construct(
67
        $connectInfo,
68
        StatementInterface $statementPrototype = null
69
    ) {
70
        parent::__construct($connectInfo);
71
72
        // set prototypes
73
        $this->statement_prototype = $statementPrototype ?: new Statement();
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    protected function extensionLoaded()/*# : bool */
80
    {
81
        return extension_loaded('PDO');
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    protected function realLastId($name)
88
    {
89
        return $this->link->lastInsertId($name);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    protected function realQuote(
96
        $string,
97
        /*# int */ $type
98
    )/*# : string */ {
99
        return $this->link->quote($string, $type);
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    protected function realConnect(array $parameters)
106
    {
107
        $link = new \PDO(
108
            $parameters['dsn'],
109
            isset($parameters['username']) ? $parameters['username'] : 'root',
110
            isset($parameters['password']) ? $parameters['password'] : null,
111
            isset($parameters['options']) ? $parameters['options'] : null
112
        );
113
        return $link;
114
    }
115
116
    /**
117
     * Disconnect the \PDO link
118
     *
119
     * {@inheritDoc}
120
     */
121
    protected function realDisconnect()
122
    {
123
    }
124
125
    /**
126
     * {@inheritDoc}
127
     */
128
    protected function realPing()/*# : bool */
129
    {
130
        try {
131
            return (bool) $this->link->query('SELECT 1');
132
        } catch (\Exception $e) {
133
            return $this->setError($e->getMessage(), $e->getCode());
134
        }
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140 View Code Duplication
    protected function realSetAttribute(/*# string */ $attribute, $value)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        if (is_string($attribute)) {
143
            $this->checkAttribute($attribute);
144
            $this->link->setAttribute(constant($attribute), $value);
145
        } else {
146
            $this->link->setAttribute($attribute, $value);
147
        }
148
        return $this;
149
    }
150
151
    /**
152
     * {@inheritDoc}
153
     */
154 View Code Duplication
    protected function realGetAttribute(/*# string */ $attribute)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        if (is_string($attribute)) {
157
            $this->checkAttribute($attribute);
158
            return $this->link->getAttribute(constant($attribute));
159
        } else {
160
            return $this->link->getAttribute($attribute);
161
        }
162
    }
163
164
    /**
165
     * {@inheritDoc}
166
     */
167
    protected function realBegin()
168
    {
169
        $this->link->beginTransaction();
170
        return $this;
171
    }
172
173
    /**
174
     * {@inheritDoc}
175
     */
176
    protected function realCommit()
177
    {
178
        $this->link->commit();
179
        return $this;
180
    }
181
182
    /**
183
     * {@inheritDoc}
184
     */
185
    protected function realRollback()
186
    {
187
        $this->link->rollBack();
188
        return $this;
189
    }
190
191
    /**
192
     * Is attribute defined ?
193
     *
194
     * @param  string $attribute
195
     * @throws LogicException
196
     * @access protected
197
     */
198
    protected function checkAttribute(/*# string */ $attribute)
199
    {
200
        if (!defined($attribute)) {
201
            throw new LogicException(
202
                Message::get(Message::DB_ATTRIBUTE_UNKNOWN, $attribute),
203
                Message::DB_ATTRIBUTE_UNKNOWN
204
            );
205
        }
206
    }
207
}
208