Completed
Push — master ( 8db522...4519e2 )
by Anton
01:48
created

CleanTables   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 30 7
1
<?php declare(strict_types=1);
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Cycle\Schema\Generator;
10
11
use Cycle\Schema\GeneratorInterface;
12
use Cycle\Schema\Registry;
13
14
/**
15
 * Declare table dropped (initiate diff calculation).
16
 */
17
final class CleanTables implements GeneratorInterface
18
{
19
    /**
20
     * @param Registry $registry
21
     * @return Registry
22
     */
23
    public function run(Registry $registry): Registry
24
    {
25
        foreach ($registry as $entity) {
26
            if (!$registry->hasTable($entity)) {
27
                continue;
28
            }
29
30
            $schema = $registry->getTableSchema($entity);
31
            if ($schema->exists()) {
32
                $state = $schema->getState();
33
34
                // clean up all indexes and columns
35
                foreach ($state->getForeignKeys() as $fk) {
36
                    $state->forgerForeignKey($fk);
37
                }
38
39
                // clean up all indexes and columns
40
                foreach ($state->getColumns() as $column) {
41
                    $state->forgetColumn($column);
42
                }
43
44
                foreach ($state->getIndexes() as $index) {
45
                    $state->forgetIndex($index);
46
                }
47
48
                $schema->setState($state);
49
            }
50
        }
51
52
        return $registry;
53
    }
54
}