ConnectTrait::ping()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 5
nop 1
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\Traits;
16
17
use Phossa2\Db\Exception\LogicException;
18
use Phossa2\Db\Interfaces\ConnectInterface;
19
20
/**
21
 * ConnectTrait
22
 *
23
 * Implementation of ConnectInterface
24
 *
25
 * @package Phossa2\Db
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     ConnectInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
trait ConnectTrait
32
{
33
    /**
34
     * the connection link
35
     *
36
     * @var    mixed
37
     * @access protected
38
     */
39
    protected $link;
40
41
    /**
42
     * connect parameters
43
     *
44
     * @var    array
45
     * @access protected
46
     */
47
    protected $connect_parameters = [];
48
49
    /**
50
     * connection attributes
51
     *
52
     * @var    array
53
     * @access protected
54
     */
55
    protected $attributes = [];
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function connect()
61
    {
62
        if ($this->isConnected()) {
63
            return $this;
64
        }
65
66
        try {
67
            $this->link = $this->realConnect($this->connect_parameters);
68
            $this->setDefaultAttributes();
69
        } catch (\Exception $e) {
70
            throw new LogicException($e->getMessage(), (int) $e->getCode(), $e);
71
        }
72
        return $this;
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function disconnect()
79
    {
80
        if ($this->isConnected()) {
81
            $this->realDisconnect();
82
            $this->link = null;
83
        }
84
        return $this;
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90
    public function isConnected()/*# : bool */
91
    {
92
        return null !== $this->link;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function ping(/*# bool */ $connect = false)/*# : bool */
99
    {
100
        // force connect
101
        if ($connect) {
102
            try {
103
                $this->connect();
104
            } catch (\Exception $e) {
105
                return $this->setError($e->getMessage(), $e->getCode());
106
            }
107
        }
108
        return $this->isConnected() ? $this->realPing() : false;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function getLink()
115
    {
116
        $this->connect();
117
        return $this->link;
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123
    public function setAttribute(/*# string */ $attribute, $value)
124
    {
125
        $this->attributes[$attribute] = $value;
126
        if ($this->isConnected()) {
127
            $this->realSetAttribute($attribute, $value);
128
        }
129
        return $this;
130
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135
    public function getAttribute(/*# string */ $attribute)
136
    {
137
        // current attribute
138
        $curr = isset($this->attributes[$attribute]) ?
139
            $this->attributes[$attribute] : null;
140
141
        if ($this->isConnected()) {
142
            $real = $this->realGetAttribute($attribute);
143
            return $real ?: $curr;
144
        } else {
145
            return $curr;
146
        }
147
    }
148
149
    /**
150
     * Set default attributes
151
     *
152
     * @access protected
153
     */
154
    protected function setDefaultAttributes()
155
    {
156
        if (!empty($this->attributes)) {
157
            foreach ($this->attributes as $attr => $val) {
158
                $this->realSetAttribute($attr, $val);
159
            }
160
        }
161
    }
162
163
    /**
164
     * Driver specific connect
165
     *
166
     * @param  array $parameters
167
     * @return mixed the link
168
     * @throws \Exception
169
     * @access protected
170
     */
171
    abstract protected function realConnect(array $parameters);
172
173
    /**
174
     * Driver specific disconnect
175
     *
176
     * @access protected
177
     */
178
    abstract protected function realDisconnect();
179
180
    /**
181
     * Driver related ping
182
     *
183
     * @return bool
184
     * @access protected
185
     */
186
    abstract protected function realPing()/*# : bool */;
187
188
    /**
189
     * Set connection specific attribute
190
     *
191
     * @param  string attribute
192
     * @param  mixed $value
193
     * @throws LogicException if attribute unknown
194
     * @access protected
195
     */
196
    abstract protected function realSetAttribute(
197
        /*# string */ $attribute,
198
        $value
199
    );
200
201
    /**
202
     * Get connection specific attribute
203
     *
204
     * @param  string attribute
205
     * @return mixed|null
206
     * @throws LogicException if attribute unknown
207
     * @access protected
208
     */
209
    abstract protected function realGetAttribute(/*# string */ $attribute);
210
211
    // from ErrorAwareInterface
212
    abstract public function setError(
213
        /*# string */ $message = '',
214
        /*# string */ $code = ''
215
    )/*# : bool */;
216
}
217