Datasource::__destruct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
/** 
4
 *  ___      _        _
5
 * | _ \__ _| |_ __ _| |__  __ _ ___ ___
6
 * |  _/ _` |  _/ _` | '_ \/ _` (_-</ -_)
7
 * |_| \__,_|\__\__,_|_.__/\__,_/__/\___|
8
 * 
9
 * This file is part of Kristuff\Patabase.
10
 * (c) Kristuff <[email protected]>
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 *
15
 * @version    1.0.1
16
 * @copyright  2017-2022 Christophe Buliard
17
 */
18
19
namespace Kristuff\Patabase;
20
21
use Kristuff\Patabase\Driver\DatabaseDriver;
22
use Kristuff\Patabase\Driver\ServerDriver;
23
use PDO;
24
25
/**
26
 * Class Datasource
27
 *
28
 * Abstract class for Database and Server class
29
 */
30
abstract class Datasource
31
{
32
33
    /**
34
     * @access protected
35
     * @var DatabaseDriver|ServerDriver   $driver     The Datasource driver
36
     */
37
    protected $driver = null;
38
39
    /**
40
     * @access protected
41
     * @var array               $settings   The driver settings
42
     */
43
    protected $settings= null;
44
45
    /**
46
     * Open a PDO connection
47
     *
48
     * @access protected
49
     * @return void
50
     */
51
    abstract protected function openConnection(): void;
52
53
    /**
54
     * Get the current driver name
55
     *
56
     * @access public
57
     * @return string
58
     */
59
    public function getDriverName(): string
60
    {
61
        return $this->driver->getDriverName();    
62
    }
63
64
    /**
65
     * Initialize the database object by creating a driver
66
     *
67
     * @access public
68
     * @param array   $settings
69
     */
70
    public function __construct(array $settings = array())
71
    {
72
        $this->settings = $settings;        
73
        $this->openConnection();
74
    }
75
76
    /**
77
     * Destructor
78
     *
79
     * @access public
80
     */
81
    public function __destruct()
82
    {
83
        $this->closeConnection();
84
    }
85
86
    /**
87
     * Has error
88
     *
89
     * @access public
90
     * @return bool     True if the last query has generated an error
91
     */
92
    public function hasError(): bool
93
    {
94
        return $this->driver->hasError();
95
    }
96
97
    /**
98
     * Get the last error code
99
     *
100
     * @access public
101
     * @return int    
102
     */
103
    public function errorCode(): int
104
    {
105
        return $this->driver->errorCode();
106
    }
107
108
    /**
109
     * Get the last error message
110
     *
111
     * @access public
112
     * @return string
113
     */
114
    public function errorMessage(): string
115
    {
116
        return $this->driver->errorMessage();
117
    }
118
119
    /**
120
     * Get the Driver
121
     *
122
     * @access public
123
     * @return 
124
     */
125
    public function getDriver()
126
    {
127
        return $this->driver;
128
    }
129
    
130
    /**
131
     * Get the PDO connection
132
     *
133
     * @access public
134
     * @return PDO
135
     */
136
    public function getConnection(): PDO
137
    {
138
        return $this->driver->getConnection();
139
    }
140
141
    /**
142
     * Close the PDO connection / reset driver
143
     *
144
     * @access public
145
     * @return void
146
     */
147
    public function closeConnection(): void
148
    {
149
        if ($this->driver){
150
            $this->driver->closeConnection();
151
            $this->driver = null;
152
        }
153
    }
154
}