Passed
Push — master ( 93e258...2028a7 )
by
unknown
16:27
created

SchemaVersionControlService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 25
c 3
b 0
f 0
dl 0
loc 58
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadSchemaFile() 0 14 3
A dumpSchema() 0 16 4
1
<?php
2
3
namespace TheCodingMachine\TDBM\SchemaVersionControl;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Schema\Schema;
7
use Symfony\Component\Yaml\Yaml;
8
9
/**
10
 * SchemaVersionControlService is the main class of this library. It provides useful methods for managing a database
11
 * schema, whether to save the current and share current configuration in YAML format, or update the schema to match
12
 * a new configuration.
13
 */
14
class SchemaVersionControlService
15
{
16
    /** @var Connection */
17
    private $connection;
18
19
    /** @var string */
20
    private $schemaFile;
21
22
    /**
23
     * SchemaVersionControlService constructor.
24
     * @param Connection $connection
25
     * @param string $schemaFile Path to config file containing top object `schema`.
26
     */
27
    public function __construct(Connection $connection, string $schemaFile)
28
    {
29
        $this->connection = $connection;
30
        $this->schemaFile = $schemaFile;
31
    }
32
33
    /**
34
     * Load schema from config file.
35
     * @return Schema
36
     */
37
    public function loadSchemaFile(): Schema
38
    {
39
        if (!file_exists($this->schemaFile)) {
40
            return new Schema();
41
        }
42
43
        $content = file_get_contents($this->schemaFile);
44
        $desc = Yaml::parse($content);
45
        if (empty($desc)) {
46
            return new Schema();
47
        }
48
49
        $builder = new SchemaBuilder();
50
        return $builder->build($desc['schema']);
51
    }
52
53
    /**
54
     * Write current database schema in config file
55
     */
56
    public function dumpSchema(): void
57
    {
58
        $schemaManager = $this->connection->createSchemaManager();
59
        $schemaConfig = $schemaManager->createSchemaConfig();
60
        $schema = $schemaManager->createSchema();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Abs...Manager::createSchema() has been deprecated: Use {@link introspectSchema()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

60
        $schema = /** @scrutinizer ignore-deprecated */ $schemaManager->createSchema();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
61
        $normalizer = new SchemaNormalizer();
62
        $desc = $normalizer->normalize($schema, $schemaConfig);
63
        $yamlSchema = Yaml::dump(['schema' => $desc], 10, 2);
64
        $directory = dirname($this->schemaFile);
65
        if (!file_exists($directory)) {
66
            if (mkdir($directory, 0666, true) === false) {
67
                throw new \RuntimeException('Could not create directory '.$directory);
68
            }
69
        }
70
        if (file_put_contents($this->schemaFile, $yamlSchema) === false) {
71
            throw new \RuntimeException('Could not edit dump file '.$this->schemaFile);
72
        }
73
    }
74
}
75