GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( fec112...b980e4 )
by Pedro
05:54 queued 03:08
created

AbsractAdapter::getPDO()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 5
nop 0
1
<?php
2
3
namespace Classes\AdaptersDriver;
4
5
use Classes\AdapterConfig\AbstractAdapter;
6
use Classes\Db\Column;
7
use Classes\Db\Constrant;
8
use Classes\Db\DbTable;
9
10
/**
11
 * Adapter com funcoes de analise das consultas
12
 *
13
 * @author Pedro Alarcao <[email protected]>
14
 * @link   https://github.com/pedro151/orm-generator
15
 */
16
abstract class AbsractAdapter
17
{
18
19
    /**
20
     * @var void variavel com tipo de dados para serem convertida
21
     */
22
    protected $dataTypesToSimple;
23
24
    /**
25
     * @type \PDO
26
     */
27
    protected $_pdo;
28
29
    /**
30
     * @var int
31
     */
32
    protected $port;
33
34
    /**
35
     * @var string
36
     */
37
    protected $host;
38
39
    /**
40
     * @type string
41
     */
42
    protected $username;
43
44
    /**
45
     * @type string
46
     */
47
    protected $password;
48
49
    /**
50
     * @var string
51
     */
52
    protected $database;
53
54
    /**
55
     * @type string
56
     */
57
    protected $socket;
58
59
    /**
60
     * @type \Classes\Db\DbTable[][]
61
     */
62
    private $objDbTables = array ();
63
64
    /**
65
     * @var AbstractAdapter
66
     */
67
    protected $config;
68
69
    /**
70
     * @type int
71
     */
72
    protected $totalTables;
73
74
    /**
75
     * analisa e popula as Foreing keys, Primary keys e dependencias do banco nos objetos
76
     */
77
    protected function parseConstrants ()
78
    {
79
        foreach ( $this->getListConstrant () as $constrant ) {
80
81
            $schema     = $constrant[ 'table_schema' ];
82
            $table_name = $constrant [ 'table_name' ];
83
            $this->populateForeignAndPrimaryKeys ( $constrant, $table_name, $schema );
84
            unset( $table_name, $schema );
85
86
            if ( $constrant[ 'constraint_type' ] == "FOREIGN KEY" ) {
87
                $schema     = $constrant[ 'foreign_schema' ];
88
                $table_name = $constrant [ 'foreign_table' ];
89
                $this->populateDependece ( $constrant, $table_name, $schema );
90
                unset( $table_name, $schema );
91
            }
92
        }
93
    }
94
95
    /**
96
     * @param array  $constrant
97
     * @param string $table_name
98
     * @param int    $schema
99
     */
100
    private function populateForeignAndPrimaryKeys ( $constrant, $table_name, $schema = 0 )
101
    {
102
        if ( $this->hasTable ( $table_name, $schema ) ) {
103
            $table = $this->getTable ( $table_name, $schema );
104
            if ( $table->hasColumn ( $constrant[ "column_name" ] ) ) {
105
                $objConstrant = Constrant::getInstance ()
106
                                         ->populate (
107
                                             array (
108
                                                 'constrant' => $constrant[ 'constraint_name' ],
109
                                                 'schema'    => $constrant[ 'foreign_schema' ],
110
                                                 'table'     => $constrant[ 'foreign_table' ],
111
                                                 'column'    => $constrant[ 'foreign_column' ],
112
                                                 'database'  => $this->database
113
                                             )
114
                                         );
115
116
                switch ( $constrant[ 'constraint_type' ] ) {
117
                    case "FOREIGN KEY":
118
                        $table->getColumn ( $constrant[ "column_name" ] )
119
                              ->addRefFk ( $objConstrant );
120
                        break;
121
                    case"PRIMARY KEY":
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
122
                        $table->getColumn ( $constrant[ "column_name" ] )
123
                              ->setPrimaryKey ( $objConstrant )
124
                              ->setSequence (
125
                                  $this->getSequence (
126
                                      $table_name,
127
                                      $constrant[ "column_name" ],
128
                                      $schema
129
                                  )
130
                              );
131
                        break;
132
                }
133
            }
134
        }
135
    }
136
137
    /**
138
     * @param array  $constrant
139
     * @param string $table_name
140
     * @param int    $schema
141
     */
142
    private function populateDependece ( $constrant, $table_name, $schema = 0 )
143
    {
144
        if ( $this->hasTable ( $table_name, $schema ) ) {
145
            $table = $this->getTable ( $table_name, $schema );
146
            if ( $table->hasColumn ( $constrant[ "foreign_column" ] ) ) {
147
                $table->getColumn ( $constrant[ "foreign_column" ] )
148
                      ->createDependece (
149
                          $constrant[ 'constraint_name' ],
150
                          $constrant[ 'table_name' ],
151
                          $constrant[ 'column_name' ],
152
                          $this->database,
153
                          $constrant[ 'table_schema' ]
154
                      );
155
            }
156
        }
157
    }
158
159
    /**
160
     * cria um Array com nome das tabelas
161
     */
162
    public function parseTables ()
163
    {
164
        if ( $this->hasTables () ) {
165
            return $this->getAllTables ();
166
        }
167
168
        foreach ( $this->getListColumns () as $table ) {
169
            $schema = $table[ 'table_schema' ];
170
            $key    = $table [ 'table_name' ];
171
            if ( !$this->hasTable ( $key, $schema ) ) {
172
                $this->createTable ( $key, $schema );
173
            }
174
175
            $column = Column::getInstance ()
176
                            ->populate (
177
                                array (
178
                                    'name'       => $table [ 'column_name' ],
179
                                    'type'       => $this->convertTypeToSimple ( $table[ 'data_type' ] ),
180
                                    'nullable'   => ( $table[ 'is_nullable' ] == 'YES' ),
181
                                    'max_length' => $table[ 'max_length' ]
182
                                )
183
                            );
184
185
            $this->getTable ( $key, $schema )
186
                 ->addColumn ( $column )
187
                 ->setNamespace (
188
                     $this->config->createClassNamespace ( $this->getTable ( $key, $schema ) )
189
                 );
190
        }
191
    }
192
193
    /**
194
     * retorna o numero total de tabelas
195
     *
196
     * @return int
197
     */
198
    abstract public function getTotalTables ();
199
200
    /**
201
     * Retorna o Nome da Sequence da tabela
202
     *
203
     * @param $table
204
     * @param $column
205
     *
206
     * @return string
207
     */
208
    abstract public function getSequence ( $table, $column, $schema = 0 );
209
210
    /**
211
     * @return array
212
     */
213
    abstract public function getListConstrant ();
214
215
    /**
216
     * @param string $str
217
     *
218
     * @return string
219
     */
220
    protected function convertTypeToPhp ( $str )
221
    {
222
        if ( isset( $this->dataTypesToPhp[ $str ] ) ) {
223
            return $this->dataTypesToPhp[ $str ];
0 ignored issues
show
Bug introduced by
The property dataTypesToPhp does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
224
        }
225
226
        return 'string';
227
    }
228
229
    protected function convertTypeToSimple ( $str )
230
    {
231
        if ( isset( $this->dataTypesToSimple[ $str ] ) ) {
232
            return $this->dataTypesToSimple[ $str ];
233
        }
234
235
        return 'string';
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    abstract public function getPDOString ();
242
243
    /**
244
     * @return string
245
     */
246
    abstract public function getPDOSocketString ();
247
248
    /**
249
     * @param     $nameTable
250
     * @param int $schema
251
     *
252
     * @return \Classes\Db\DbTable
253
     */
254
    public function createTable ( $nameTable, $schema = 0 )
255
    {
256
        $this->objDbTables[ $schema ][ trim ( $nameTable ) ] = DbTable::getInstance ()
257
                                                                      ->populate (
258
                                                                          array (
259
                                                                              'table'    => $nameTable,
260
                                                                              'schema'   => $schema,
261
                                                                              'database' => $this->database
262
                                                                          )
263
                                                                      );
264
265
        return $this;
266
    }
267
268
    /**
269
     * Retorna um Array Assoc com a chave com nome da tabela e o valor com objeto tables
270
     *
271
     * @return \Classes\Db\DbTable[]
272
     */
273
    public function getTables ( $schema = 0 )
274
    {
275
        if ( !isset( $this->objDbTables[ $schema ] ) ) {
276
            return array ();
277
        }
278
279
        return $this->objDbTables[ $schema ];
280
    }
281
282
    public function getAllTables ()
283
    {
284
        return $this->objDbTables;
285
    }
286
287
    public function hasTables ()
288
    {
289
        return !empty( $this->objDbTables );
290
    }
291
292
    /**
293
     * retorna a tabela especifica
294
     *
295
     * @param string $nameTable Nome da tabela
296
     *
297
     * @return \Classes\Db\DbTable
298
     */
299
    public function getTable ( $nameTable, $schema = 0 )
300
    {
301
        return $this->objDbTables[ $schema ][ trim ( $nameTable ) ];
302
    }
303
304
    /**
305
     * @param string     $nameTable
306
     * @param int|string $schema
307
     *
308
     * @return bool
309
     */
310
    public function hasTable ( $nameTable, $schema = 0 )
311
    {
312
        return isset( $this->objDbTables[ $schema ][ trim ( $nameTable ) ] );
313
    }
314
315
    /**
316
     * retorna multiplos arrays com dados da column em array
317
     *
318
     * @return array[]
319
     */
320
    abstract public function getListColumns ();
321
322
    /**
323
     * Retorna um Array com nome das tabelas
324
     *
325
     * @return string[]
326
     */
327
    abstract public function getListNameTable ();
328
329
    /**
330
     * @param \Classes\AdapterConfig\AbstractAdapter $adapterConfig
331
     */
332
    public function __construct ( AbstractAdapter $adapterConfig )
333
    {
334
        $this->config   = $adapterConfig;
335
        $this->host     = $adapterConfig->getHost ();
336
        $this->database = $adapterConfig->getDatabase ();
337
        $this->port     = $adapterConfig->hasPort () ? $adapterConfig->getPort () : $this->port;
338
        $this->username = $adapterConfig->getUser ();
339
        $this->password = $adapterConfig->getPassword ();
340
        $this->socket   = $adapterConfig->getSocket ();
341
342
    }
343
344
    /**
345
     * Executa as consultas do banco de dados
346
     */
347
    public function runDatabase ()
348
    {
349
        $this->parseTables ();
350
        $this->parseConstrants ();
351
    }
352
353
    /**
354
     *
355
     * @return \PDO
356
     */
357
    public function getPDO ()
358
    {
359
        if ( is_null ( $this->_pdo ) ) {
360
            if ( !empty( $this->socket ) ) {
361
                $pdoString = $this->getPDOSocketString ();
362
            }
363
            else {
364
                $pdoString = $this->getPDOString ();
365
            }
366
367
            try {
368
                $this->_pdo = new \PDO (
369
                    $pdoString, $this->username, $this->password
370
                );
371
            } catch ( \Exception $e ) {
372
                die ( "pdo error: " . $e->getMessage () . "\n" );
0 ignored issues
show
Coding Style Compatibility introduced by
The method getPDO() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
373
            }
374
        }
375
376
        return $this->_pdo;
377
    }
378
}
379