DbFacade   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
c 0
b 0
f 0
dl 0
loc 173
rs 10
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A connectToServer() 0 3 1
A connect() 0 12 2
A connectToDatabase() 0 3 1
A getServerName() 0 3 1
A setCurrentDbName() 0 3 1
A selectDatabase() 0 5 1
A getDatabaseOptions() 0 9 3
A di() 0 3 1
A breadcrumbs() 0 9 3
A connectToSchema() 0 3 1
1
<?php
2
3
namespace Lagdo\DbAdmin\Db;
4
5
use Jaxon\App\View\ViewRenderer;
6
use Jaxon\Di\Container;
7
use Lagdo\DbAdmin\Admin\Admin;
8
use Lagdo\DbAdmin\Db\Facades\AbstractFacade;
9
use Lagdo\DbAdmin\Driver\Utils\Utils;
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 ViewRenderer $viewRenderer
55
     */
56
    public function __construct(protected Container $di, protected Utils $utils,
57
        protected ViewRenderer $viewRenderer)
58
    {
59
        // Make the translator available into views
60
        $viewRenderer->share('trans', $utils->trans);
61
        $this->breadcrumbs = new Breadcrumbs();
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getServerName(): string
68
    {
69
        return $this->dbServer;
70
    }
71
72
    /**
73
     * Get the breadcrumbs object
74
     *
75
     * @param bool $withDb
76
     *
77
     * @return Breadcrumbs
78
     */
79
    public function breadcrumbs(bool $withDb = false): Breadcrumbs
80
    {
81
        if ($withDb) {
82
            $this->breadcrumbs->clear();
83
            if ($this->dbName) {
84
                $this->breadcrumbs->item("<i><b>$this->dbName</b></i>");
85
            }
86
        }
87
        return $this->breadcrumbs;
88
    }
89
90
    /**
91
     * @return Container
92
     */
93
    public function di(): Container
94
    {
95
        return $this->di;
96
    }
97
98
    /**
99
     * Set the current database
100
     *
101
     * @param string $server    The selected server
102
     * @param string $database  The database name
103
     * @param string $schema    The database schema
104
     *
105
     * @return void
106
     */
107
    public function selectDatabase(string $server, string $database = '', string $schema = '')
108
    {
109
        $this->dbServer = $server;
110
        $this->dbName = $database;
111
        $this->dbSchema = $schema;
112
    }
113
114
    /**
115
     * Set the current database name
116
     *
117
     * @param string $database  The database name
118
     *
119
     * @return void
120
     */
121
    public function setCurrentDbName(string $database)
122
    {
123
        $this->dbName = $database;
124
    }
125
126
    /**
127
     * Connect to a database server
128
     *
129
     * @param string $server    The selected server
130
     * @param string $database  The database name
131
     * @param string $schema    The database schema
132
     *
133
     * @return void
134
     */
135
    private function connect(string $server, string $database = '', string $schema = '')
136
    {
137
        // Prevent multiple calls.
138
        if (!$this->driver) {
139
            // Save the selected server in the di container.
140
            $this->di->val('dbadmin_config_server', $server);
141
            // The DI is now able to return the corresponding driver.
142
            $this->driver = $this->di->get(AppDriver::class);
143
            $this->admin = $this->di->get(Admin::class);
144
        }
145
        // Open the selected database
146
        $this->driver->open($database, $schema);
147
    }
148
149
    /**
150
     * @return void
151
     */
152
    public function connectToServer()
153
    {
154
        $this->connect($this->dbServer);
155
    }
156
157
    /**
158
     * @return void
159
     */
160
    public function connectToDatabase()
161
    {
162
        $this->connect($this->dbServer, $this->dbName);
163
    }
164
165
    /**
166
     * @return void
167
     */
168
    public function connectToSchema()
169
    {
170
        $this->connect($this->dbServer, $this->dbName, $this->dbSchema);
171
    }
172
173
    /**
174
     * @param array $options
175
     *
176
     * @return array
177
     */
178
    public function getDatabaseOptions(array $options): array
179
    {
180
        if ($this->dbName !== '') {
181
            $options['database'] = $this->dbName;
182
        }
183
        if ($this->dbSchema !== '') {
184
            $options['schema'] = $this->dbSchema;
185
        }
186
        return $options;
187
    }
188
}
189