| Total Complexity | 50 |
| Total Lines | 300 |
| Duplicated Lines | 0 % |
| Changes | 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 | foreach (SchemaHelper::getAuditTableColumns() as $columnName => $struct) { |
||
| 185 | $auditTable->addColumn($columnName, $struct['type'], $struct['options']); |
||
| 186 | } |
||
| 187 | |||
| 188 | // Add indices to audit table |
||
| 189 | foreach (SchemaHelper::getAuditTableIndices($auditTablename) as $columnName => $struct) { |
||
| 190 | if ('primary' === $struct['type']) { |
||
| 191 | $auditTable->setPrimaryKey([$columnName]); |
||
| 192 | } else { |
||
| 193 | $auditTable->addIndex( |
||
| 194 | [$columnName], |
||
| 195 | $struct['name'], |
||
| 196 | [], |
||
| 197 | $this->isIndexLengthLimited($columnName, $connection) ? ['lengths' => [191]] : [] |
||
| 198 | ); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | return $schema; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Ensures an audit table's structure is valid. |
||
| 208 | * |
||
| 209 | * @throws SchemaException |
||
| 210 | */ |
||
| 211 | public function updateAuditTable(string $entity, Table $table, ?Schema $schema = null): Schema |
||
| 212 | { |
||
| 213 | /** @var StorageService $storageService */ |
||
| 214 | $storageService = $this->provider->getStorageServiceForEntity($entity); |
||
| 215 | $connection = $storageService->getEntityManager()->getConnection(); |
||
| 216 | |||
| 217 | $schemaManager = $connection->getSchemaManager(); |
||
| 218 | if (null === $schema) { |
||
| 219 | $schema = $schemaManager->createSchema(); |
||
| 220 | } |
||
| 221 | |||
| 222 | $table = $schema->getTable($table->getName()); |
||
| 223 | $columns = $schemaManager->listTableColumns($table->getName()); |
||
| 224 | |||
| 225 | // process columns |
||
| 226 | $this->processColumns($table, $columns, SchemaHelper::getAuditTableColumns()); |
||
| 227 | |||
| 228 | // process indices |
||
| 229 | $this->processIndices($table, SchemaHelper::getAuditTableIndices($table->getName()), $connection); |
||
| 230 | |||
| 231 | return $schema; |
||
| 232 | } |
||
| 233 | |||
| 234 | private function processColumns(Table $table, array $columns, array $expectedColumns): void |
||
| 235 | { |
||
| 236 | $processed = []; |
||
| 237 | |||
| 238 | foreach ($columns as $column) { |
||
| 239 | if (\array_key_exists($column->getName(), $expectedColumns)) { |
||
| 240 | // column is part of expected columns |
||
| 241 | $table->dropColumn($column->getName()); |
||
| 242 | $table->addColumn($column->getName(), $expectedColumns[$column->getName()]['type'], $expectedColumns[$column->getName()]['options']); |
||
| 243 | } else { |
||
| 244 | // column is not part of expected columns so it has to be removed |
||
| 245 | $table->dropColumn($column->getName()); |
||
| 246 | } |
||
| 247 | |||
| 248 | $processed[] = $column->getName(); |
||
| 249 | } |
||
| 250 | |||
| 251 | foreach ($expectedColumns as $columnName => $options) { |
||
| 252 | if (!\in_array($columnName, $processed, true)) { |
||
| 253 | // expected column in not part of concrete ones so it's a new column, we need to add it |
||
| 254 | $table->addColumn($columnName, $options['type'], $options['options']); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @throws SchemaException |
||
| 261 | */ |
||
| 262 | private function processIndices(Table $table, array $expectedIndices, Connection $connection): void |
||
| 277 | ); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * MySQL < 5.7.7 and MariaDb < 10.2.2 index length requirements. |
||
| 284 | * |
||
| 285 | * @see https://github.com/doctrine/dbal/issues/3419 |
||
| 286 | */ |
||
| 287 | private function isIndexLengthLimited(string $name, Connection $connection): bool |
||
| 318 | } |
||
| 319 | } |
||
| 320 |