Passed
Push — master ( 68f720...5e727b )
by Paul
10:53
created

Install::dropTables()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 11
c 2
b 0
f 1
dl 0
loc 17
ccs 0
cts 12
cp 0
rs 9.6111
cc 5
nc 4
nop 1
crap 30
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Database\Query;
6
use GeminiLabs\SiteReviews\Database\SqlSchema;
7
8
class Install
9
{
10
    /**
11
     * @param bool $isNetworkDeactivating
12
     * @return void
13
     */
14
    public function deactivate($isNetworkDeactivating)
15
    {
16
        if (!$isNetworkDeactivating) {
17
            $this->dropForeignConstraints();
18
            delete_option(glsr()->prefix.'activated');
0 ignored issues
show
Bug introduced by
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

18
            /** @scrutinizer ignore-call */ 
19
            delete_option(glsr()->prefix.'activated');
Loading history...
19
            return;
20
        }
21
        foreach ($this->sites() as $siteId) {
22
            switch_to_blog($siteId);
0 ignored issues
show
Bug introduced by
The function switch_to_blog was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

22
            /** @scrutinizer ignore-call */ 
23
            switch_to_blog($siteId);
Loading history...
23
            $this->dropForeignConstraints();
24
            delete_option(glsr()->prefix.'activated');
25
            restore_current_blog();
0 ignored issues
show
Bug introduced by
The function restore_current_blog was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

25
            /** @scrutinizer ignore-call */ 
26
            restore_current_blog();
Loading history...
26
        }
27
    }
28
29
    /**
30
     * @return void
31
     */
32
    public function dropForeignConstraints()
33
    {
34
        glsr(SqlSchema::class)->dropForeignConstraint('assigned_posts', 'assigned_posts_post_id');
35
        glsr(SqlSchema::class)->dropForeignConstraint('assigned_terms', 'assigned_terms_term_id');
36
        glsr(SqlSchema::class)->dropForeignConstraint('assigned_users', 'assigned_users_user_id');
37
        glsr(SqlSchema::class)->dropForeignConstraint('ratings', 'assigned_posts_review_id');
38
    }
39
40
    /**
41
     * @param bool $dropAll
42
     * @return void
43
     */
44
    public function dropTables($dropAll = true)
45
    {
46
        $tables = $this->tables();
47
        if (is_multisite() && $dropAll) {
0 ignored issues
show
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

47
        if (/** @scrutinizer ignore-call */ is_multisite() && $dropAll) {
Loading history...
48
            foreach ($this->sites() as $siteId) {
49
                switch_to_blog($siteId);
0 ignored issues
show
Bug introduced by
The function switch_to_blog was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

49
                /** @scrutinizer ignore-call */ 
50
                switch_to_blog($siteId);
Loading history...
50
                $tables = array_unique(array_merge($tables, $this->tables()));
51
                delete_option('glsr_db_version');
0 ignored issues
show
Bug introduced by
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

51
                /** @scrutinizer ignore-call */ 
52
                delete_option('glsr_db_version');
Loading history...
52
                restore_current_blog();
0 ignored issues
show
Bug introduced by
The function restore_current_blog was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

52
                /** @scrutinizer ignore-call */ 
53
                restore_current_blog();
Loading history...
53
            }
54
        }
55
        foreach ($tables as $table) {
56
            glsr(Database::class)->dbQuery(
57
                glsr(Query::class)->sql("DROP TABLE IF EXISTS {$table}")
58
            );
59
        }
60
        delete_option('glsr_db_version');
61
    }
62
63
    /**
64
     * @return void
65
     */
66 7
    public function run()
67
    {
68 7
        require_once ABSPATH.'/wp-admin/includes/plugin.php';
69 7
        if (is_plugin_active_for_network(plugin_basename(glsr()->file))) {
0 ignored issues
show
Bug introduced by
The function plugin_basename was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

69
        if (is_plugin_active_for_network(/** @scrutinizer ignore-call */ plugin_basename(glsr()->file))) {
Loading history...
Bug introduced by
The function is_plugin_active_for_network was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

69
        if (/** @scrutinizer ignore-call */ is_plugin_active_for_network(plugin_basename(glsr()->file))) {
Loading history...
70
            foreach ($this->sites() as $siteId) {
71
                $this->runOnSite($siteId);
72
            }
73
            return;
74
        }
75 7
        $this->install();
76 7
    }
77
78
    /**
79
     * @param int $siteId
80
     * @return void
81
     */
82
    public function runOnSite($siteId)
83
    {
84
        switch_to_blog($siteId);
0 ignored issues
show
Bug introduced by
The function switch_to_blog was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

84
        /** @scrutinizer ignore-call */ 
85
        switch_to_blog($siteId);
Loading history...
85
        $this->install();
86
        restore_current_blog();
0 ignored issues
show
Bug introduced by
The function restore_current_blog was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

86
        /** @scrutinizer ignore-call */ 
87
        restore_current_blog();
Loading history...
87
    }
88
89
    /**
90
     * @return void
91
     */
92 7
    protected function createRoleCapabilities()
93
    {
94 7
        glsr(Role::class)->resetAll();
95 7
    }
96
97
    /**
98
     * @return void
99
     */
100 7
    protected function createTables()
101
    {
102 7
        glsr(SqlSchema::class)->createTables();
103 7
        glsr(SqlSchema::class)->addForeignConstraints();
104 7
    }
105
106
    /**
107
     * @return void
108
     */
109 7
    protected function deleteInvalidAssignments()
110
    {
111 7
        glsr(Database::class)->deleteInvalidPostAssignments();
112 7
        glsr(Database::class)->deleteInvalidTermAssignments();
113 7
        glsr(Database::class)->deleteInvalidUserAssignments();
114 7
    }
115
116
    /**
117
     * @return void
118
     */
119 7
    protected function install()
120
    {
121 7
        $this->createRoleCapabilities();
122 7
        $this->createTables();
123 7
        $this->deleteInvalidAssignments();
124 7
    }
125
126
    /**
127
     * @return array
128
     */
129
    protected function sites()
130
    {
131
        return get_sites([
0 ignored issues
show
Bug introduced by
The function get_sites was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

131
        return /** @scrutinizer ignore-call */ get_sites([
Loading history...
132
            'fields' => 'ids',
133
            'network_id' => get_current_network_id(),
0 ignored issues
show
Bug introduced by
The function get_current_network_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

133
            'network_id' => /** @scrutinizer ignore-call */ get_current_network_id(),
Loading history...
134
        ]);
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    protected function tables()
141
    {
142
        return [
143
            glsr(SqlSchema::class)->table('assigned_posts'),
144
            glsr(SqlSchema::class)->table('assigned_terms'),
145
            glsr(SqlSchema::class)->table('assigned_users'),
146
            glsr(SqlSchema::class)->table('ratings'),
147
        ];
148
    }
149
}
150