Passed
Push — master ( 18d41c...f9af5f )
by Paul
13:52 queued 07:13
created

Migrate_5_9_0   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 65.79%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 11
eloc 32
c 4
b 2
f 0
dl 0
loc 75
ccs 25
cts 38
cp 0.6579
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A repairDatabase() 0 12 3
A run() 0 5 1
A migrateDatabase() 0 16 3
A install() 0 8 1
A isDatabaseVersionUpdated() 0 9 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Migrations;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Database\CountManager;
7
use GeminiLabs\SiteReviews\Database\Query;
8
use GeminiLabs\SiteReviews\Database\SqlSchema;
9
use GeminiLabs\SiteReviews\Install;
10
11
class Migrate_5_9_0
12
{
13
    /**
14
     * @return bool
15
     */
16 14
    public function migrateDatabase()
17
    {
18 14
        $table = glsr(SqlSchema::class)->table('ratings');
19 14
        if ($this->isDatabaseVersionUpdated()) {
20 14
            return true;
21
        }
22
        glsr(Database::class)->dbQuery(glsr(Query::class)->sql("
23
            ALTER TABLE {$table}
24
            ADD terms tinyint(1) NOT NULL DEFAULT '1'
25
            AFTER url
26
        "));
27
        if ($this->isDatabaseVersionUpdated()) { // @phpstan-ignore-line
28
            return true; // check again after updating the database
29
        }
30
        glsr_log()->error(sprintf('Database table [%s] could not be altered, column [terms] not added.', $table));
31
        return false;
32
    }
33
34
    /**
35
     * @return void
36
     */
37 14
    public function repairDatabase()
38
    {
39 14
        require_once ABSPATH.'/wp-admin/includes/plugin.php';
40 14
        if (is_plugin_active_for_network(plugin_basename(glsr()->file))) {
41
            foreach (glsr(Install::class)->sites() as $siteId) {
42
                switch_to_blog($siteId);
43
                $this->install();
44
                restore_current_blog();
45
            }
46
            return;
47
        }
48 14
        $this->install();
49 14
    }
50
51
    /**
52
     * @return bool
53
     */
54 14
    public function run()
55
    {
56 14
        $this->repairDatabase(); // fix orphaned rows and foreign indexes
57 14
        glsr(CountManager::class)->recalculate();
58 14
        return $this->migrateDatabase();
59
    }
60
61
    /**
62
     * @return void
63
     */
64 14
    protected function install()
65
    {
66 14
        glsr(SqlSchema::class)->createTables();
67 14
        glsr(SqlSchema::class)->addForeignConstraints();
68 14
        glsr(Database::class)->deleteInvalidPostAssignments();
69 14
        glsr(Database::class)->deleteInvalidTermAssignments();
70 14
        glsr(Database::class)->deleteInvalidUserAssignments();
71 14
        glsr(Database::class)->deleteInvalidReviews();
72 14
    }
73
74
    /**
75
     * @return bool
76
     */
77 14
    protected function isDatabaseVersionUpdated()
78
    {
79 14
        if (glsr(SqlSchema::class)->columnExists('ratings', 'terms')) {
80 14
            if (!glsr(Database::class)->version('1.1')) {
81
                update_option(glsr()->prefix.'db_version', '1.1');
82
            }
83 14
            return true;
84
        }
85
        return false;
86
    }
87
88
}
89