Passed
Push — master ( f0530b...3e604e )
by Paul
14:52 queued 08:11
created

Migrate_5_3_0::migrateDatabase()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.7861

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
ccs 5
cts 12
cp 0.4167
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 4.7861
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Migrations;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\SqlSchema;
7
8
class Migrate_5_3_0
9
{
10
    /**
11
     * @return bool
12
     */
13 7
    public function run()
14
    {
15 7
        $this->migrateDatabase();
16 7
        return true;
17
    }
18
19
    /**
20
     * @return void
21
     */
22 7
    protected function fixDatabaseVersion()
23
    {
24 7
        $databaseVersion = get_option(glsr()->prefix.'db_version');
25 7
        if ('5.2' === $databaseVersion) {
26
            $version = '1.0'; // @compat
27
            if (glsr(SqlSchema::class)->columnExists('ratings', 'terms')) {
28
                $version = Application::DB_VERSION;
29
            }
30
            update_option(glsr()->prefix.'db_version', $version);
31
        }
32 7
    }
33
34
    /**
35
     * @return void
36
     */
37 7
    protected function migrateDatabase()
38
    {
39 7
        require_once ABSPATH.'/wp-admin/includes/plugin.php';
40 7
        if (!is_plugin_active_for_network(plugin_basename(glsr()->file))) {
41 7
            $this->fixDatabaseVersion();
42 7
            return;
43
        }
44
        $sites = get_sites([
45
            'fields' => 'ids',
46
            'network_id' => get_current_network_id(),
47
        ]);
48
        foreach ($sites as $siteId) {
49
            switch_to_blog($siteId);
50
            $this->fixDatabaseVersion();
51
            restore_current_blog();
52
        }
53
    }
54
}
55