Passed
Push — master ( 6b35fa...ccb079 )
by Paul
10:24 queued 05:13
created

Migrate::runMigrations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0052

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 11
c 1
b 1
f 0
nc 3
nop 1
dl 0
loc 16
ccs 11
cts 12
cp 0.9167
crap 3.0052
rs 9.9
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use DirectoryIterator;
6
use GeminiLabs\SiteReviews\Application;
7
use GeminiLabs\SiteReviews\Database\OptionManager;
8
use GeminiLabs\SiteReviews\Helper;
9
use GeminiLabs\SiteReviews\Helpers\Arr;
10
11
class Migrate
12
{
13
    /**
14
     * @var string
15
     */
16
    public $currentVersion;
17
18
    /**
19
     * @var string
20
     */
21
    public $transientKey;
22
23 1
    public function __construct()
24
    {
25 1
        $this->currentVersion = $this->getCurrentVersion();
26 1
        $this->transientKey = Application::PREFIX.'migrations';
27 1
    }
28
29
    /**
30
     * @return bool
31
     */
32 1
    public function isMigrationNeeded()
33
    {
34 1
        $transient = get_transient($this->transientKey);
35 1
        if (false === $transient || !isset($transient[glsr()->version])) {
36
            $transient = [
37 1
                glsr()->version => !empty($this->getNewMigrationFiles()),
38
            ];
39 1
            set_transient($this->transientKey, $transient);
40
        }
41 1
        return Helper::castToBool($transient[glsr()->version]);
42
    }
43
44
    /**
45
     * @return void
46
     */
47 1
    public function run()
48
    {
49 1
        $this->runMigrations($this->getNewMigrationFiles());
50 1
    }
51
52
    /**
53
     * @return bool
54
     */
55
    public function runAll()
56
    {
57
        $this->runMigrations($this->getMigrationFiles());
58
    }
59
60
    /**
61
     * @return string
62
     */
63 1
    protected function getCurrentVersion()
64
    {
65 1
        $fallback = '0.0.0';
66 1
        $majorVersions = [4, 3, 2, 1];
67 1
        foreach ($majorVersions as $majorVersion) {
68 1
            $settings = get_option(OptionManager::databaseKey($majorVersion));
69 1
            $version = Arr::get($settings, 'version', $fallback);
70 1
            if (Helper::isGreaterThan($version, $fallback)) {
71 1
                return $version;
72
            }
73
        }
74 1
        return $fallback;
75
    }
76
77
    /**
78
     * @return array
79
     */
80 1
    protected function getMigrationFiles()
81
    {
82 1
        $files = [];
83 1
        $dir = glsr()->path('plugin/Modules/Migrations');
84 1
        if (is_dir($dir)) {
85 1
            $iterator = new DirectoryIterator($dir);
86 1
            foreach ($iterator as $fileinfo) {
87 1
                if ($fileinfo->isFile()) {
88 1
                    $files[] = $fileinfo->getFilename();
89
                }
90
            }
91 1
            natsort($files);
92
        }
93 1
        return $files;
94
    }
95
96
    /**
97
     * @return array
98
     */
99 1
    protected function getNewMigrationFiles()
100
    {
101 1
        $files = $this->getMigrationFiles();
102 1
        foreach ($files as $index => $file) {
103 1
            $className = str_replace('.php', '', $file);
104 1
            $migrationVersion = str_replace(['Migrate_', '_'], ['', '.'], $className);
105 1
            $suffix = preg_replace('/[\d.]+(.+)?/', '${1}', glsr()->version); // allow alpha/beta versions
106 1
            if (Helper::isGreaterThanOrEqual($this->currentVersion, $migrationVersion.$suffix)) {
107 1
                unset($files[$index]);
108
            }
109
        }
110 1
        return $files;
111
    }
112
113
    /**
114
     * @return void
115
     */
116 1
    protected function runMigrations(array $files)
117
    {
118 1
        if (empty($files)) {
119
            return;
120
        }
121
        array_walk($files, function ($file) {
122 1
            $className = str_replace('.php', '', $file);
123 1
            glsr('Modules\\Migrations\\'.$className)->run();
124 1
            $versionMigrated = str_replace(['Migrate_', '_'], ['v','.'], $className);
125 1
            glsr_log()->debug('migration completed for '.$versionMigrated);
126 1
        });
127 1
        if ($this->currentVersion !== glsr()->version) {
128 1
            $this->updateVersionFrom($this->currentVersion);
129
        }
130 1
        glsr(OptionManager::class)->set('last_migration_run', current_time('timestamp'));
131 1
        delete_transient($this->transientKey);
132 1
    }
133
134
    /**
135
     * @param string $previousVersion
136
     * @return void
137
     */
138 1
    protected function updateVersionFrom($previousVersion)
139
    {
140 1
        glsr(OptionManager::class)->set('version', glsr()->version);
141 1
        glsr(OptionManager::class)->set('version_upgraded_from', $previousVersion);
142 1
    }
143
}
144