Metadata   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 189
rs 10
c 0
b 0
f 0
wmc 15
lcom 2
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 4 1
A getMetadata() 0 27 4
A filterColumn() 0 12 4
queryFields() 0 1 ?
A getFields() 0 4 1
A getFieldsList() 0 4 1
A getPK() 0 4 1
A getWithDefault() 0 4 1
A getAutoFields() 0 4 1
1
<?php
2
3
/**
4
 * KumbiaPHP web & app Framework.
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file LICENSE.txt.
10
 * It is also available through the world-wide-web at this URL:
11
 * http://wiki.kumbiaphp.com/Licencia
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @category   Kumbia
17
 *
18
 * @copyright  2005 - 2020  Kumbia Team (http://www.kumbiaphp.com)
19
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
20
 */
21
namespace Kumbia\ActiveRecord\Metadata;
22
23
use Kumbia\ActiveRecord\Db;
24
25
/**
26
 * Metadata de tabla.
27
 */
28
abstract class Metadata
29
{
30
    /**
31
     * Singleton de metadata.
32
     *
33
     * @var self[]
34
     */
35
    private static $instances = [];
36
37
    /**
38
     * Descripción de los campos.
39
     *
40
     * @var array
41
     */
42
    protected $fields = [];
43
44
    /**
45
     * Lista de campos.
46
     *
47
     * @var string[]
48
     */
49
    protected $fieldsList = [];
50
51
    /**
52
     * Clave primaria.
53
     *
54
     * @var string
55
     */
56
    protected $pk;
57
58
    /**
59
     * Campos con valor predeterminado.
60
     *
61
     * @var string[]
62
     */
63
    protected $withDefault = [];
64
65
    /**
66
     * Campos con valor autogenerado.
67
     *
68
     * @var string[]
69
     */
70
    protected $autoFields = [];
71
72
    /**
73
     * Metadata de la tabla.
74
     *
75
     * @param  string     $database
76
     * @param  string     $table
77
     * @param  string     $schema
78
     * 
79
     * @return self
80
     */
81
    public static function get(string $database, string $table, string $schema = ''): self
82
    {
83
        return self::$instances["$database.$table.$schema"] ?? self::getMetadata($database, $table, $schema);
84
    }
85
86
    /**
87
     * Obtiene la metadata de la tabla
88
     * Y la cachea si esta en producción.
89
     *
90
     * @param  string     $database
91
     * @param  string     $table
92
     * @param  string     $schema
93
     * 
94
     * @return self
95
     */
96
    private static function getMetadata(string $database, string $table, string $schema): self
97
    {
98
        $key = "$database.$table.$schema";
99
        //TODO añadir cache propia
100
101
        if (\PRODUCTION && self::$instances[$key] = \unserialize(\Cache::driver()->get($key, 'ActiveRecord.Metadata'))) {
102
            return self::$instances[$key];
103
        }
104
        
105
        $pdo = Db::get($database);
106
107
        $driverClass = __NAMESPACE__."\\".\ucfirst($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)).'Metadata';
108
109
        self::$instances[$key] = new $driverClass($pdo, $table, $schema);
110
111
        // Cachea los metadatos
112
        if (\PRODUCTION) {
113
            \Cache::driver()->save(
114
                \serialize(self::$instances[$key]),
115
                \Config::get('config.application.metadata_lifetime'),
116
                $key,
117
                'ActiveRecord.Metadata'
118
            );
119
        }
120
121
        return self::$instances[$key];
122
    }
123
124
    /**
125
     * Constructor.
126
     *
127
     * @param \PDO   $pdo      base de datos
128
     * @param string $table    tabla
129
     * @param string $schema   squema
130
     */
131
    private function __construct(\PDO $pdo, string $table, string $schema = '')
132
    {
133
        $this->fields     = $this->queryFields($pdo, $table, $schema);
134
        $this->fieldsList = \array_keys($this->fields);
0 ignored issues
show
Documentation Bug introduced by
It seems like \array_keys($this->fields) of type array<integer,integer|string> is incompatible with the declared type array<integer,string> of property $fieldsList.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
135
    }
136
137
    /**
138
     * Permite el filtrado de columna en PK, por Defecto y Autogenerado.
139
     *
140
     * @param array     $meta  información de la columna
141
     * @param string    $field nombre      de la columna
142
     */
143
    protected function filterColumn(array $meta, string $field): void
144
    {
145
        if ($meta['Key'] === 'PRI') {
146
            $this->pk = $field;
147
        }
148
        if ($meta['Default']) {
149
            $this->withDefault[] = $field;
150
        }
151
        if ($meta['Auto']) {
152
            $this->autoFields[] = $field;
153
        }
154
    }
155
156
    /**
157
     * Consultar los campos de la tabla en la base de datos.
158
     *
159
     * @param  \PDO    $pdo      base de datos
160
     * @param  string  $table    tabla
161
     * @param  string  $schema   squema
162
     * 
163
     * @return array
164
     */
165
    abstract protected function queryFields(\PDO $pdo, string $table, string $schema = ''): array;
166
167
    /**
168
     * Obtiene la descripción de los campos.
169
     *
170
     * @return string[]
171
     */
172
    public function getFields(): array
173
    {
174
        return $this->fields;
175
    }
176
177
    /**
178
     * Obtiene la lista de campos.
179
     *
180
     * @return string[]
181
     */
182
    public function getFieldsList(): array
183
    {
184
        return $this->fieldsList;
185
    }
186
187
    /**
188
     * Obtiene la clave primaria.
189
     *
190
     * @return string
191
     */
192
    public function getPK(): string
193
    {
194
        return $this->pk;
195
    }
196
197
    /**
198
     * Obtiene los campos con valor predeterminado.
199
     *
200
     * @return string[]
201
     */
202
    public function getWithDefault(): array
203
    {
204
        return $this->withDefault;
205
    }
206
207
    /**
208
     * Obtiene los campos con valor generado automatico.
209
     *
210
     * @return string[]
211
     */
212
    public function getAutoFields(): array
213
    {
214
        return $this->autoFields;
215
    }
216
}
217