Passed
Push — main ( 2c1d44...33f9f8 )
by Thierry
23:08 queued 20:19
created

DbFacade::getDatabaseOptions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 10
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\Package;
10
11
/**
12
 * Facade to calls to the database functions
13
 */
14
class DbFacade extends AbstractFacade
15
{
16
    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...
17
    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...
18
    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...
19
    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...
20
    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...
21
    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...
22
    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...
23
    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...
24
    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...
25
    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...
26
27
    /**
28
     * The breadcrumbs items
29
     *
30
     * @var Breadcrumbs
31
     */
32
    protected $breadcrumbs;
33
34
    /**
35
     * @var string
36
     */
37
    protected $dbServer;
38
39
    /**
40
     * @var string
41
     */
42
    protected $dbName;
43
44
    /**
45
     * @var string
46
     */
47
    protected $dbSchema;
48
49
    /**
50
     * The constructor
51
     *
52
     * @param Container $di
53
     * @param Utils $utils
54
     * @param Package $package
55
     */
56
    public function __construct(protected Container $di, protected Utils $utils,
57
        protected Package $package)
58
    {
59
        // Make the translator available into views
60
        $this->package->view()->share('trans', $utils->trans);
61
        $this->breadcrumbs = new Breadcrumbs();
62
    }
63
64
    /**
65
     * Get the breadcrumbs object
66
     *
67
     * @param bool $withDb
68
     *
69
     * @return Breadcrumbs
70
     */
71
    public function breadcrumbs(bool $withDb = false): Breadcrumbs
72
    {
73
        if ($withDb) {
74
            $this->breadcrumbs->clear();
75
            if ($this->dbName) {
76
                $this->breadcrumbs->item("<i><b>$this->dbName</b></i>");
77
            }
78
        }
79
        return $this->breadcrumbs;
80
    }
81
82
    /**
83
     * @return Container
84
     */
85
    public function di(): Container
86
    {
87
        return $this->di;
88
    }
89
90
    /**
91
     * Set the current database
92
     *
93
     * @param string $server    The selected server
94
     * @param string $database  The database name
95
     * @param string $schema    The database schema
96
     *
97
     * @return void
98
     */
99
    public function selectDatabase(string $server, string $database = '', string $schema = '')
100
    {
101
        $this->dbServer = $server;
102
        $this->dbName = $database;
103
        $this->dbSchema = $schema;
104
    }
105
106
    /**
107
     * Set the current database name
108
     *
109
     * @param string $database  The database name
110
     *
111
     * @return void
112
     */
113
    public function setCurrentDbName(string $database)
114
    {
115
        $this->dbName = $database;
116
    }
117
118
    /**
119
     * Connect to a database server
120
     *
121
     * @param string $server    The selected server
122
     * @param string $database  The database name
123
     * @param string $schema    The database schema
124
     *
125
     * @return void
126
     */
127
    private function connect(string $server, string $database = '', string $schema = '')
128
    {
129
        // Prevent multiple calls.
130
        if (!$this->driver) {
131
            // Save the selected server options in the di container.
132
            $this->di->val('dbadmin_config_driver', $this->package->getServerDriver($server));
133
            $this->di->val('dbadmin_config_options', $this->package->getServerOptions($server));
134
            $this->driver = $this->di->get(CallbackDriver::class);
135
            $this->admin = $this->di->get(Admin::class);
136
        }
137
        // Open the selected database
138
        $this->driver->open($database, $schema);
139
    }
140
141
    /**
142
     * @return void
143
     */
144
    public function connectToServer()
145
    {
146
        $this->connect($this->dbServer);
147
    }
148
149
    /**
150
     * @return void
151
     */
152
    public function connectToDatabase()
153
    {
154
        $this->connect($this->dbServer, $this->dbName);
155
    }
156
157
    /**
158
     * @return void
159
     */
160
    public function connectToSchema()
161
    {
162
        $this->connect($this->dbServer, $this->dbName, $this->dbSchema);
163
    }
164
165
    /**
166
     * @return array
167
     */
168
    public function getServerOptions(): array
169
    {
170
        return $this->package->getServerOptions($this->dbServer);
171
    }
172
173
    /**
174
     * @return array
175
     */
176
    public function getDatabaseOptions(): array
177
    {
178
        $options = $this->getServerOptions();
179
        if ($this->dbName !== '') {
180
            $options['database'] = $this->dbName;
181
        }
182
        if ($this->dbSchema !== '') {
183
            $options['schema'] = $this->dbSchema;
184
        }
185
        return $options;
186
    }
187
}
188