Passed
Push — master ( 1511a6...137b49 )
by Anton
03:21
created

ResetTables   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 32 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 ResetTables 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
                $state->setPrimaryKeys([]);
49
50
                $schema->setState($state);
51
            }
52
        }
53
54
        return $registry;
55
    }
56
}