Passed
Push — main ( f332df...379075 )
by Thierry
02:57
created

DbFacade::bcdb()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Db;
4
5
use Jaxon\Di\Container;
6
use Lagdo\DbAdmin\Admin\Admin;
7
use Lagdo\DbAdmin\Db\Facades\AbstractFacade;
8
use Lagdo\DbAdmin\Driver\Utils\Utils;
9
use Lagdo\DbAdmin\Driver\DriverInterface;
10
use Lagdo\DbAdmin\Package;
11
12
/**
13
 * Facade to calls to the database functions
14
 */
15
class DbFacade extends AbstractFacade
16
{
17
    use Traits\ServerTrait;
0 ignored issues
show
Bug introduced by
The trait Lagdo\DbAdmin\Db\Traits\ServerTrait requires the property $trans which is not provided by Lagdo\DbAdmin\Db\DbFacade.
Loading history...
18
    use Traits\UserTrait;
0 ignored issues
show
Bug introduced by
The trait Lagdo\DbAdmin\Db\Traits\UserTrait requires the property $trans which is not provided by Lagdo\DbAdmin\Db\DbFacade.
Loading history...
19
    use Traits\DatabaseTrait;
0 ignored issues
show
Bug introduced by
The trait Lagdo\DbAdmin\Db\Traits\DatabaseTrait requires the property $trans which is not provided by Lagdo\DbAdmin\Db\DbFacade.
Loading history...
20
    use Traits\TableTrait;
0 ignored issues
show
introduced by
The trait Lagdo\DbAdmin\Db\Traits\TableTrait requires some properties which are not provided by Lagdo\DbAdmin\Db\DbFacade: $trans, $input
Loading history...
21
    use Traits\SelectTrait;
0 ignored issues
show
Bug introduced by
The trait Lagdo\DbAdmin\Db\Traits\SelectTrait requires the property $input which is not provided by Lagdo\DbAdmin\Db\DbFacade.
Loading history...
22
    use Traits\QueryTrait;
0 ignored issues
show
introduced by
The trait Lagdo\DbAdmin\Db\Traits\QueryTrait requires some properties which are not provided by Lagdo\DbAdmin\Db\DbFacade: $trans, $input
Loading history...
23
    use Traits\ViewTrait;
0 ignored issues
show
introduced by
The trait Lagdo\DbAdmin\Db\Traits\ViewTrait requires some properties which are not provided by Lagdo\DbAdmin\Db\DbFacade: $trans, $input
Loading history...
24
    use Traits\CommandTrait;
0 ignored issues
show
Bug introduced by
The trait Lagdo\DbAdmin\Db\Traits\CommandTrait requires the property $trans which is not provided by Lagdo\DbAdmin\Db\DbFacade.
Loading history...
25
    use Traits\ExportTrait;
0 ignored issues
show
Bug introduced by
The trait Lagdo\DbAdmin\Db\Traits\ExportTrait requires the property $trans which is not provided by Lagdo\DbAdmin\Db\DbFacade.
Loading history...
26
    use Traits\ImportTrait;
0 ignored issues
show
Bug introduced by
The trait Lagdo\DbAdmin\Db\Traits\ImportTrait requires the property $trans which is not provided by Lagdo\DbAdmin\Db\DbFacade.
Loading history...
27
28
    /**
29
     * The breadcrumbs items
30
     *
31
     * @var Breadcrumbs
32
     */
33
    protected $breadcrumbs;
34
35
    /**
36
     * @var Container
37
     */
38
    protected $di;
39
40
    /**
41
     * @var string
42
     */
43
    protected $dbServer;
44
45
    /**
46
     * @var string
47
     */
48
    protected $dbName;
49
50
    /**
51
     * @var string
52
     */
53
    protected $dbSchema;
54
55
    /**
56
     * The constructor
57
     *
58
     * @param Container $di
59
     * @param Utils $utils
60
     * @param Package $package
61
     */
62
    public function __construct(Container $di, Utils $utils, Package $package)
63
    {
64
        $this->di = $di;
65
        $this->utils = $utils;
66
        $this->package = $package;
67
        // Make the translator available into views
68
        $this->package->view()->share('trans', $this->utils->trans);
69
70
        $this->breadcrumbs = new Breadcrumbs();
71
    }
72
73
    /**
74
     * @return Utils|null
75
     */
76
    public function utils(): Utils
77
    {
78
        return $this->utils;
79
    }
80
81
    /**
82
     * Get the breadcrumbs object
83
     *
84
     * @param bool $withDb
85
     *
86
     * @return Breadcrumbs
87
     */
88
    public function breadcrumbs(bool $withDb = false): Breadcrumbs
89
    {
90
        if ($withDb) {
91
            $this->breadcrumbs->clear();
92
            if ($this->dbName) {
93
                $this->breadcrumbs->item("<i><b>$this->dbName</b></i>");
94
            }
95
        }
96
        return $this->breadcrumbs;
97
    }
98
99
    /**
100
     * @return Container
101
     */
102
    public function di(): Container
103
    {
104
        return $this->di;
105
    }
106
107
    /**
108
     * Set the current database
109
     *
110
     * @param string $server    The selected server
111
     * @param string $database  The database name
112
     * @param string $schema    The database schema
113
     *
114
     * @return void
115
     */
116
    public function selectDatabase(string $server, string $database = '', string $schema = '')
117
    {
118
        $this->dbServer = $server;
119
        $this->dbName = $database;
120
        $this->dbSchema = $schema;
121
    }
122
123
    /**
124
     * Set the current database name
125
     *
126
     * @param string $database  The database name
127
     *
128
     * @return void
129
     */
130
    public function setCurrentDbName(string $database)
131
    {
132
        $this->dbName = $database;
133
    }
134
135
    /**
136
     * Get the current server
137
     *
138
     * @return string
139
     */
140
    public function getCurrentServer(): string
141
    {
142
        return $this->dbServer;
143
    }
144
145
    /**
146
     * Connect to a database server
147
     *
148
     * @param string $server    The selected server
149
     * @param string $database  The database name
150
     * @param string $schema    The database schema
151
     *
152
     * @return void
153
     */
154
    public function connect(string $server, string $database = '', string $schema = '')
155
    {
156
        // Prevent multiple calls.
157
        if (!$this->driver) {
158
            // Save the selected server options in the di container.
159
            $this->di->val('dbadmin_config_driver', $this->package->getServerDriver($server));
160
            $this->di->val('dbadmin_config_options', $this->package->getServerOptions($server));
161
            $this->driver = $this->di->get(DriverInterface::class);
162
            $this->admin = $this->di->get(Admin::class);
163
        }
164
        // Open the selected database
165
        $this->driver->open($database, $schema);
166
    }
167
168
    /**
169
     * @return void
170
     */
171
    public function connectToServer()
172
    {
173
        $this->connect($this->dbServer);
174
    }
175
176
    /**
177
     * @return void
178
     */
179
    public function connectToDatabase()
180
    {
181
        $this->connect($this->dbServer, $this->dbName);
182
    }
183
184
    /**
185
     * @return void
186
     */
187
    public function connectToSchema()
188
    {
189
        $this->connect($this->dbServer, $this->dbName, $this->dbSchema);
190
    }
191
192
    /**
193
     * @return array
194
     */
195
    public function getServerOptions(): array
196
    {
197
        return $this->package->getServerOptions($this->dbServer);
198
    }
199
200
    /**
201
     * Get the remembered queries
202
     *
203
     * @return array
204
     */
205
    public function queries(): array
206
    {
207
        return $this->driver->queries();
208
    }
209
}
210