Passed
Push — master ( 849dc0...25ac3b )
by Fran
03:04
created

MigrationService::generateMigrationFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
3
namespace PSFS\services;
4
5
use Propel\Generator\Config\GeneratorConfig;
6
use Propel\Generator\Manager\MigrationManager;
7
use Propel\Generator\Model\Database;
8
use Propel\Generator\Model\IdMethod;
9
use PSFS\base\Logger;
10
use PSFS\base\types\SimpleService;
11
use PSFS\base\types\traits\Generator\PropelHelperTrait;
12
use Symfony\Component\Console\Output\Output;
13
14
class MigrationService extends SimpleService
15
{
16
    use PropelHelperTrait;
17
18
19
    /**
20
     * @param string $module
21
     * @param string $path
22
     * @return array
23
     * @throws \PSFS\base\exception\GeneratorException
24
     */
25
    public function getConnectionManager(string $module, string $path): array
26
    {
27
        $modulePath = str_replace(CORE_DIR . DIRECTORY_SEPARATOR, '', $path . $module);
28
        $generatorConfig = $this->getConfigGenerator($modulePath);
29
30
        $manager = new MigrationManager();
31
        $manager->setGeneratorConfig($generatorConfig);
32
        $manager->setSchemas($this->getSchemas(
33
            $generatorConfig->getSection('paths')['schemaDir'],
34
            $generatorConfig->getSection('generator')['recursive'])
35
        );
36
        $manager->setConnections($generatorConfig->getBuildConnections());
37
        $manager->setMigrationTable($generatorConfig->getConfigProperty('migrations.tableName'));
0 ignored issues
show
Bug introduced by
It seems like $generatorConfig->getCon...'migrations.tableName') can also be of type null; however, parameter $migrationTable of Propel\Generator\Manager...er::setMigrationTable() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

37
        $manager->setMigrationTable(/** @scrutinizer ignore-type */ $generatorConfig->getConfigProperty('migrations.tableName'));
Loading history...
38
        $manager->setWorkingDirectory($generatorConfig->getSection('paths')['migrationDir']);
39
        return [$manager, $generatorConfig];
40
    }
41
42
    /**
43
     * @param MigrationManager $manager
44
     * @param GeneratorConfig $generatorConfig
45
     * @param Database $appDatabase
46
     * @param array $connections
47
     * @param bool $debugLogger
48
     * @return array
49
     */
50
    public function checkSourceDatabase(MigrationManager $manager, GeneratorConfig $generatorConfig, Database $appDatabase, array $connections, bool $debugLogger = false): array
51
    {
52
        $name = $appDatabase->getName();
53
        $params = $connections[$name] ?? [];
54
        if (!$params) {
55
            Logger::log(sprintf('<info>No connection configured for database "%s"</info>', $name));
56
        }
57
58
        if ($debugLogger) {
59
            Logger::log(sprintf('Connecting to database "%s" using DSN "%s"', $name, $params['dsn']));
60
        }
61
62
        list($conn, $platform) = $this->getPlatformAndConnection($manager, $name, $generatorConfig);
63
64
        $appDatabase->setPlatform($platform);
65
66
        if ($platform && !$platform->supportsMigrations()) {
67
            Logger::log(sprintf('Skipping database "%s" since vendor "%s" does not support migrations', $name, $platform->getDatabaseType()));
68
            return [null, 0];
69
        }
70
71
        $additionalTables = [];
72
        foreach ($appDatabase->getTables() as $table) {
73
            if ($table->getSchema() && $table->getSchema() != $appDatabase->getSchema()) {
74
                $additionalTables[] = $table;
75
            }
76
        }
77
78
        $database = new Database($name);
79
        $database->setPlatform($platform);
80
        $database->setSchema($appDatabase->getSchema());
81
        $database->setDefaultIdMethod(IdMethod::NATIVE);
82
83
        $parser = $generatorConfig->getConfiguredSchemaParser($conn, $name);
84
        $nbTables = $parser->parse($database, $additionalTables);
85
86
        if ($debugLogger) {
87
            Logger::log(sprintf('%d tables found in database "%s"', $nbTables, $name), Output::VERBOSITY_VERBOSE);
88
        }
89
        return [$database, $nbTables];
90
    }
91
92
    /**
93
     * @param MigrationManager $manager
94
     * @param string|null $name
95
     * @param GeneratorConfig $generatorConfig
96
     * @return array
97
     */
98
    public function getPlatformAndConnection(MigrationManager $manager, ?string $name, GeneratorConfig $generatorConfig): array
99
    {
100
        $conn = $manager->getAdapterConnection($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $datasource of Propel\Generator\Manager...:getAdapterConnection() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

100
        $conn = $manager->getAdapterConnection(/** @scrutinizer ignore-type */ $name);
Loading history...
101
        $platform = $generatorConfig->getConfiguredPlatform($conn, $name);
102
        return [$conn, $platform];
103
    }
104
105
    /**
106
     * @param MigrationManager $manager
107
     * @param array $migrationsUp
108
     * @param array $migrationsDown
109
     * @param GeneratorConfig $generatorConfig
110
     * @return void
111
     */
112
    public function generateMigrationFile(MigrationManager $manager, array $migrationsUp, array $migrationsDown, GeneratorConfig $generatorConfig): void
113
    {
114
        $timestamp = time();
115
        $migrationFileName = $manager->getMigrationFileName($timestamp);
116
        $migrationClassBody = $manager->getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp);
117
118
        $file = $generatorConfig->getSection('paths')['migrationDir'] . DIRECTORY_SEPARATOR . $migrationFileName;
119
        file_put_contents($file, $migrationClassBody);
120
121
        Logger::log(sprintf('"%s" file successfully created.', $file));
122
    }
123
}
124