Test Failed
Push — develop ( 33b521...cdbd76 )
by Paul
10:16 queued 21s
created

TableStats   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 1
b 0
f 0
dl 0
loc 44
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dropForeignConstraints() 0 3 1
A structure() 0 13 1
A addForeignConstraints() 0 3 1
A removeInvalidRows() 0 6 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database\Tables;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Database\Query;
7
use GeminiLabs\SiteReviews\Database\Tables\AbstractTable;
8
9
class TableStats extends AbstractTable
10
{
11
    public string $name = 'stats';
12
13
    public function addForeignConstraints(): void
14
    {
15
        $this->addForeignConstraint('rating_id', $this->table('ratings'), 'ID');
16
    }
17
18
    public function dropForeignConstraints(): void
19
    {
20
        $this->dropForeignConstraint('rating_id', $this->table('ratings'));
21
    }
22
23
    public function removeInvalidRows(): void
24
    {
25
        glsr(Database::class)->dbSafeQuery(
26
            glsr(Query::class)->sql("
27
                DELETE t
28
                FROM {$this->tablename} AS t
29
                LEFT JOIN table|ratings AS r ON (r.ID = t.rating_id)
30
                WHERE r.ID IS NULL
31
            ")
32
        );
33
    }
34
35
    /**
36
     * WordPress codex says there must be two spaces between PRIMARY KEY and the key definition.
37
     *
38
     * @see https://codex.wordpress.org/Creating_Tables_with_Plugins
39
     */
40
    public function structure(): string
41
    {
42
        return glsr(Query::class)->sql("
43
            CREATE TABLE {$this->tablename} (
44
                ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
45
                rating_id bigint(20) unsigned NOT NULL,
46
                continent varchar(10) NOT NULL,
47
                country varchar(10) NOT NULL,
48
                region varchar(10) NOT NULL,
49
                city varchar(250) NOT NULL,
50
                PRIMARY KEY  (ID),
51
                UNIQUE KEY glsr_stats_rating_id_unique (rating_id)
52
            ) ENGINE=InnoDB {$this->db->get_charset_collate()};
53
        ");
54
    }
55
}
56