Passed
Pull Request — master (#288)
by
unknown
03:15
created

SchemaVersionControlService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
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
     * Get the current schema used in database.
35
     * @return Schema
36
     */
37
    public function getCurrentSchema(): Schema
38
    {
39
        return $this->connection->getSchemaManager()->createSchema();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::getSchemaManager() has been deprecated: Use {@see createSchemaManager()} instead. ( Ignorable by Annotation )

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

39
        return /** @scrutinizer ignore-deprecated */ $this->connection->getSchemaManager()->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...
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

39
        return /** @scrutinizer ignore-deprecated */ $this->connection->getSchemaManager()->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...
40
    }
41
42
    /**
43
     * Load schema from config file.
44
     * @return Schema
45
     */
46
    public function loadSchemaFile(): Schema
47
    {
48
        if (!file_exists($this->schemaFile)) {
49
            return new Schema();
50
        }
51
52
        $content = file_get_contents($this->schemaFile);
53
        $desc = Yaml::parse($content);
54
        if (empty($desc)) {
55
            return new Schema();
56
        }
57
58
        $builder = new SchemaBuilder();
59
        return $builder->build($desc['schema']);
60
    }
61
62
    /**
63
     * Write current database schema in config file
64
     */
65
    public function dumpSchema(): void
66
    {
67
        $schema = $this->getCurrentSchema();
68
        $normalizer = new SchemaNormalizer();
69
        $desc = $normalizer->normalize($schema);
70
        $yamlSchema = Yaml::dump(['schema' => $desc], 10, 2);
71
        $directory = dirname($this->schemaFile);
72
        if (!file_exists($directory)) {
73
            if (mkdir($directory, 0666, true) === false) {
74
                throw new \RuntimeException('Could not create directory '.$directory);
75
            }
76
        }
77
        if (file_put_contents($this->schemaFile, $yamlSchema) === false) {
78
            throw new \RuntimeException('Could not edit dump file '.$this->schemaFile);
79
        }
80
    }
81
}
82