| Total Complexity | 67 |
| Total Lines | 395 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like SchemaManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SchemaManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class SchemaManager |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var DoctrineProvider |
||
| 22 | */ |
||
| 23 | private $provider; |
||
| 24 | |||
| 25 | public function __construct(DoctrineProvider $provider) |
||
| 26 | { |
||
| 27 | $this->provider = $provider; |
||
| 28 | } |
||
| 29 | |||
| 30 | public function updateAuditSchema(?array $sqls = null, ?callable $callback = null): void |
||
| 31 | { |
||
| 32 | // TODO: FIXME will create the same schema on all connections |
||
| 33 | if (null === $sqls) { |
||
| 34 | $sqls = $this->getUpdateAuditSchemaSql(); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** @var StorageService[] $storageServices */ |
||
| 38 | $storageServices = $this->provider->getStorageServices(); |
||
| 39 | foreach ($sqls as $name => $queries) { |
||
| 40 | foreach ($queries as $index => $sql) { |
||
| 41 | $statement = $storageServices[$name]->getEntityManager()->getConnection()->prepare($sql); |
||
| 42 | $statement->execute(); |
||
| 43 | |||
| 44 | if (null !== $callback) { |
||
| 45 | $callback([ |
||
| 46 | 'total' => \count($sqls), |
||
| 47 | 'current' => $index, |
||
| 48 | ]); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns an array of audit table names indexed by entity FQN. |
||
| 56 | * Only auditable entities are considered. |
||
| 57 | * |
||
| 58 | * @throws \Doctrine\ORM\ORMException |
||
| 59 | */ |
||
| 60 | public function getAuditableTableNames(EntityManagerInterface $entityManager): array |
||
| 61 | { |
||
| 62 | $metadataDriver = $entityManager->getConfiguration()->getMetadataDriverImpl(); |
||
| 63 | $entities = []; |
||
| 64 | if (null !== $metadataDriver) { |
||
| 65 | $entities = $metadataDriver->getAllClassNames(); |
||
| 66 | } |
||
| 67 | $audited = []; |
||
| 68 | foreach ($entities as $entity) { |
||
| 69 | if ($this->provider->isAuditable($entity)) { |
||
| 70 | $audited[$entity] = $entityManager->getClassMetadata($entity)->getTableName(); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | ksort($audited); |
||
| 74 | |||
| 75 | return $audited; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getUpdateAuditSchemaSql(): array |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Creates an audit table. |
||
| 155 | */ |
||
| 156 | public function createAuditTable(string $entity, Table $table, ?Schema $schema = null): Schema |
||
| 157 | { |
||
| 158 | /** @var StorageService $storageService */ |
||
| 159 | $storageService = $this->provider->getStorageServiceForEntity($entity); |
||
| 160 | $connection = $storageService->getEntityManager()->getConnection(); |
||
| 161 | |||
| 162 | if (null === $schema) { |
||
| 163 | $schemaManager = $connection->getSchemaManager(); |
||
| 164 | $schema = $schemaManager->createSchema(); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** @var Configuration $configuration */ |
||
| 168 | $configuration = $this->provider->getConfiguration(); |
||
| 169 | |||
| 170 | $auditTablename = preg_replace( |
||
| 171 | sprintf('#^([^\.]+\.)?(%s)$#', preg_quote($table->getName(), '#')), |
||
| 172 | sprintf( |
||
| 173 | '$1%s$2%s', |
||
| 174 | preg_quote($configuration->getTablePrefix(), '#'), |
||
| 175 | preg_quote($configuration->getTableSuffix(), '#') |
||
| 176 | ), |
||
| 177 | $table->getName() |
||
| 178 | ); |
||
| 179 | |||
| 180 | if (null !== $auditTablename && !$schema->hasTable($auditTablename)) { |
||
| 181 | $auditTable = $schema->createTable($auditTablename); |
||
| 182 | |||
| 183 | // Add columns to audit table |
||
| 184 | $isJsonSupported = $this->isJsonSupported($connection); |
||
| 185 | foreach (SchemaHelper::getAuditTableColumns() as $columnName => $struct) { |
||
| 186 | if (DoctrineHelper::getDoctrineType('JSON') === $struct['type'] && $isJsonSupported) { |
||
| 187 | $type = DoctrineHelper::getDoctrineType('TEXT'); |
||
| 188 | } else { |
||
| 189 | $type = $struct['type']; |
||
| 190 | } |
||
| 191 | |||
| 192 | $auditTable->addColumn($columnName, $type, $struct['options']); |
||
| 193 | } |
||
| 194 | |||
| 195 | // Add indices to audit table |
||
| 196 | foreach (SchemaHelper::getAuditTableIndices($auditTablename) as $columnName => $struct) { |
||
| 197 | if ('primary' === $struct['type']) { |
||
| 198 | $auditTable->setPrimaryKey([$columnName]); |
||
| 199 | } else { |
||
| 200 | $auditTable->addIndex( |
||
| 201 | [$columnName], |
||
| 202 | $struct['name'], |
||
| 203 | [], |
||
| 204 | $this->isIndexLengthLimited($columnName, $connection) ? ['lengths' => [191]] : [] |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | return $schema; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Ensures an audit table's structure is valid. |
||
| 215 | * |
||
| 216 | * @throws SchemaException |
||
| 217 | */ |
||
| 218 | public function updateAuditTable(string $entity, Table $table, ?Schema $schema = null): Schema |
||
| 219 | { |
||
| 220 | /** @var StorageService $storageService */ |
||
| 221 | $storageService = $this->provider->getStorageServiceForEntity($entity); |
||
| 222 | $connection = $storageService->getEntityManager()->getConnection(); |
||
| 223 | |||
| 224 | $schemaManager = $connection->getSchemaManager(); |
||
| 225 | if (null === $schema) { |
||
| 226 | $schema = $schemaManager->createSchema(); |
||
| 227 | } |
||
| 228 | |||
| 229 | $table = $schema->getTable($table->getName()); |
||
| 230 | $columns = $schemaManager->listTableColumns($table->getName()); |
||
| 231 | |||
| 232 | // process columns |
||
| 233 | $this->processColumns($table, $columns, SchemaHelper::getAuditTableColumns(), $connection); |
||
| 234 | |||
| 235 | // process indices |
||
| 236 | $this->processIndices($table, SchemaHelper::getAuditTableIndices($table->getName()), $connection); |
||
| 237 | |||
| 238 | return $schema; |
||
| 239 | } |
||
| 240 | |||
| 241 | private function processColumns(Table $table, array $columns, array $expectedColumns, Connection $connection): void |
||
| 242 | { |
||
| 243 | $processed = []; |
||
| 244 | |||
| 245 | $isJsonSupported = $this->isJsonSupported($connection); |
||
| 246 | foreach ($columns as $column) { |
||
| 247 | if (\array_key_exists($column->getName(), $expectedColumns)) { |
||
| 248 | // column is part of expected columns |
||
| 249 | $table->dropColumn($column->getName()); |
||
| 250 | |||
| 251 | if (DoctrineHelper::getDoctrineType('JSON') === $expectedColumns[$column->getName()]['type'] && $isJsonSupported) { |
||
| 252 | $type = DoctrineHelper::getDoctrineType('TEXT'); |
||
| 253 | } else { |
||
| 254 | $type = $expectedColumns[$column->getName()]['type']; |
||
| 255 | } |
||
| 256 | |||
| 257 | $table->addColumn($column->getName(), $type, $expectedColumns[$column->getName()]['options']); |
||
| 258 | } else { |
||
| 259 | // column is not part of expected columns so it has to be removed |
||
| 260 | $table->dropColumn($column->getName()); |
||
| 261 | } |
||
| 262 | |||
| 263 | $processed[] = $column->getName(); |
||
| 264 | } |
||
| 265 | |||
| 266 | foreach ($expectedColumns as $columnName => $options) { |
||
| 267 | if (!\in_array($columnName, $processed, true)) { |
||
| 268 | // expected column in not part of concrete ones so it's a new column, we need to add it |
||
| 269 | if (DoctrineHelper::getDoctrineType('JSON') === $options['type'] && $isJsonSupported) { |
||
| 270 | $type = DoctrineHelper::getDoctrineType('TEXT'); |
||
|
|
|||
| 271 | } else { |
||
| 272 | $type = $options['type']; |
||
| 273 | } |
||
| 274 | |||
| 275 | $table->addColumn($columnName, $options['type'], $options['options']); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @throws SchemaException |
||
| 282 | */ |
||
| 283 | private function processIndices(Table $table, array $expectedIndices, Connection $connection): void |
||
| 298 | ); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * MySQL < 5.7.7 and MariaDb < 10.2.2 index length requirements. |
||
| 305 | * |
||
| 306 | * @see https://github.com/doctrine/dbal/issues/3419 |
||
| 307 | */ |
||
| 308 | private function isIndexLengthLimited(string $name, Connection $connection): bool |
||
| 309 | { |
||
| 310 | $columns = SchemaHelper::getAuditTableColumns(); |
||
| 311 | if ( |
||
| 312 | !isset($columns[$name]) |
||
| 313 | || $columns[$name]['type'] !== DoctrineHelper::getDoctrineType('STRING') |
||
| 314 | || ( |
||
| 315 | isset($columns[$name]['options'], $columns[$name]['options']['length']) |
||
| 316 | && $columns[$name]['options']['length'] < 191 |
||
| 317 | ) |
||
| 318 | ) { |
||
| 319 | return false; |
||
| 320 | } |
||
| 321 | |||
| 322 | $version = $this->getServerVersion($connection); |
||
| 323 | |||
| 324 | if (null === $version) { |
||
| 325 | return false; |
||
| 326 | } |
||
| 327 | |||
| 328 | $mariadb = false !== mb_stripos($version, 'mariadb'); |
||
| 329 | if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.2', '<')) { |
||
| 330 | return true; |
||
| 331 | } |
||
| 332 | |||
| 333 | if (!$mariadb && version_compare($this->getOracleMysqlVersionNumber($version), '5.7.7', '<')) { |
||
| 334 | return true; |
||
| 335 | } |
||
| 336 | |||
| 337 | return false; |
||
| 338 | } |
||
| 339 | |||
| 340 | private function getServerVersion(Connection $connection): ?string |
||
| 341 | { |
||
| 342 | $wrappedConnection = $connection->getWrappedConnection(); |
||
| 343 | |||
| 344 | if ($wrappedConnection instanceof ServerInfoAwareConnection) { |
||
| 345 | return $wrappedConnection->getServerVersion(); |
||
| 346 | } |
||
| 347 | |||
| 348 | return null; |
||
| 349 | } |
||
| 350 | |||
| 351 | private function isJsonSupported(Connection $connection): bool |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get a normalized 'version number' from the server string |
||
| 371 | * returned by Oracle MySQL servers. |
||
| 372 | * |
||
| 373 | * @param string $versionString Version string returned by the driver, i.e. '5.7.10' |
||
| 374 | * |
||
| 375 | * @copyright Doctrine team |
||
| 376 | */ |
||
| 377 | private function getOracleMysqlVersionNumber(string $versionString): string |
||
| 378 | { |
||
| 379 | preg_match( |
||
| 380 | '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', |
||
| 381 | $versionString, |
||
| 382 | $versionParts |
||
| 383 | ); |
||
| 384 | |||
| 385 | $majorVersion = $versionParts['major']; |
||
| 386 | $minorVersion = $versionParts['minor'] ?? 0; |
||
| 387 | $patchVersion = $versionParts['patch'] ?? null; |
||
| 388 | |||
| 389 | if ('5' === $majorVersion && '7' === $minorVersion && null === $patchVersion) { |
||
| 390 | $patchVersion = '9'; |
||
| 391 | } |
||
| 392 | |||
| 393 | return $majorVersion.'.'.$minorVersion.'.'.$patchVersion; |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Detect MariaDB server version, including hack for some mariadb distributions |
||
| 398 | * that starts with the prefix '5.5.5-'. |
||
| 399 | * |
||
| 400 | * @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial' |
||
| 401 | * |
||
| 402 | * @copyright Doctrine team |
||
| 403 | */ |
||
| 404 | private function getMariaDbMysqlVersionNumber(string $versionString): string |
||
| 413 | } |
||
| 414 | } |
||
| 415 |