Passed
Push — master ( 6e10d3...1791d6 )
by Anton
02:07
created

SyncTables   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 18 5
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\Exception\SyncException;
12
use Cycle\Schema\GeneratorInterface;
13
use Cycle\Schema\Registry;
14
use Spiral\Database\Schema\Reflector;
15
16
/**
17
 * Sync table schemas with database.
18
 */
19
final class SyncTables implements GeneratorInterface
20
{
21
    // Readonly tables must be included form the sync with database
22
    public const READONLY_SCHEMA = 'readonlySchema';
23
24
    /**
25
     * @param Registry $registry
26
     * @return Registry
27
     *
28
     * @throws SyncException
29
     */
30
    public function run(Registry $registry): Registry
31
    {
32
        $reflector = new Reflector();
33
        foreach ($registry as $entity) {
34
            if (!$registry->hasTable($entity) || $entity->getOptions()->has(self::READONLY_SCHEMA)) {
35
                continue;
36
            }
37
38
            $reflector->addTable($registry->getTableSchema($entity));
39
        }
40
41
        try {
42
            $reflector->run();
43
        } catch (\Throwable $e) {
44
            throw new SyncException($e->getMessage(), $e->getCode(), $e);
45
        }
46
47
        return $registry;
48
    }
49
}