Completed
Branch dev (623cfe)
by Greg
04:05
created

DwStats::checkForServerTotalsTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * /classes/DomainMOD/DwStats.php
4
 *
5
 * This file is part of DomainMOD, an open source domain and internet asset manager.
6
 * Copyright (c) 2010-2017 Greg Chetcuti <[email protected]>
7
 *
8
 * Project: http://domainmod.org   Author: http://chetcuti.com
9
 *
10
 * DomainMOD is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
11
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * DomainMOD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
15
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along with DomainMOD. If not, see
18
 * http://www.gnu.org/licenses/.
19
 *
20
 */
21
//@formatter:off
22
namespace DomainMOD;
23
24
class DwStats
25
{
26
    public $system;
27
    public $time;
28
29
    public function __construct()
30
    {
31
        $this->system = new System();
32
        $this->time = new Time();
33
    }
34
35
    public function updateServerStats($result)
36
    {
37
        foreach ($result as $row) {
38
39
            $total_dw_accounts = $this->getTotals($row->id, 'dw_accounts');
40
            $total_dw_dns_zones = $this->getTotals($row->id, 'dw_dns_zones');
41
            $total_dw_dns_records = $this->getTotals($row->id, 'dw_dns_records');
42
            $this->updateServerTotals($row->id, $total_dw_accounts, $total_dw_dns_zones, $total_dw_dns_records);
43
44
        }
45
    }
46
47 View Code Duplication
    public function getTotals($server_id, $table)
48
    {
49
        $pdo = $this->system->db();
50
51
        $stmt = $pdo->prepare("
52
            SELECT count(*)
53
            FROM `" . $table . "`
54
            WHERE server_id = :server_id");
55
        $stmt->bindValue('server_id', $server_id, \PDO::PARAM_INT);
56
        $stmt->execute();
57
58
        return $stmt->fetchColumn();
59
    }
60
61 View Code Duplication
    public function updateServerTotals($server_id, $total_accounts, $total_dns_zones, $total_dns_records)
62
    {
63
        $pdo = $this->system->db();
64
65
        $stmt = $pdo->prepare("
66
            UPDATE dw_servers
67
            SET dw_accounts = :total_accounts,
68
                dw_dns_zones = :total_dns_zones,
69
                dw_dns_records = :total_dns_records
70
            WHERE id = :server_id");
71
        $stmt->bindValue('total_accounts', $total_accounts, \PDO::PARAM_INT);
72
        $stmt->bindValue('total_dns_zones', $total_dns_zones, \PDO::PARAM_INT);
73
        $stmt->bindValue('total_dns_records', $total_dns_records, \PDO::PARAM_INT);
74
        $stmt->bindValue('server_id', $server_id, \PDO::PARAM_INT);
75
        $stmt->execute();
76
    }
77
78
    public function updateDwTotalsTable()
79
    {
80
        $accounts = new DwAccounts();
81
        $zones = new DwZones();
82
        $records = new DwRecords();
83
84
        $this->deleteTotalsTable();
85
        $this->recreateDwTotalsTable();
86
        $total_dw_servers = $this->getTotalDwServers();
87
        $total_dw_accounts = $accounts->getTotalDwAccounts();
88
        $total_dw_zones = $zones->getTotalDwZones();
89
        $total_dw_records = $records->getTotalDwRecords();
90
        $this->updateTable($total_dw_servers, $total_dw_accounts, $total_dw_zones, $total_dw_records);
91
    }
92
93
    public function deleteTotalsTable()
94
    {
95
        $this->system->db()->query("DROP TABLE IF EXISTS dw_server_totals");
96
    }
97
98
    public function recreateDwTotalsTable()
99
    {
100
        $this->system->db()->query("
101
            CREATE TABLE IF NOT EXISTS `dw_server_totals` (
102
                `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
103
                `dw_servers` INT(10) UNSIGNED NOT NULL,
104
                `dw_accounts` INT(10) UNSIGNED NOT NULL,
105
                `dw_dns_zones` INT(10) UNSIGNED NOT NULL,
106
                `dw_dns_records` INT(10) UNSIGNED NOT NULL,
107
                `insert_time` DATETIME NOT NULL,
108
                PRIMARY KEY  (`id`)
109
            ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;");
110
    }
111
112
    public function getTotalDwServers()
113
    {
114
        return $this->system->db()->query("
115
            SELECT count(*)
116
            FROM `dw_servers`")->fetchColumn();
117
    }
118
119 View Code Duplication
    public function updateTable($total_dw_servers, $total_dw_accounts, $total_dw_dns_zones, $total_dw_records)
120
    {
121
        $pdo = $this->system->db();
122
123
        $stmt = $pdo->prepare("
124
            INSERT INTO dw_server_totals
125
            (dw_servers, dw_accounts, dw_dns_zones, dw_dns_records, insert_time)
126
            VALUES
127
            (:total_dw_servers, :total_dw_accounts, :total_dw_dns_zones, :total_dw_records, :insert_time)");
128
        $stmt->bindValue('total_dw_servers', $total_dw_servers, \PDO::PARAM_INT);
129
        $stmt->bindValue('total_dw_accounts', $total_dw_accounts, \PDO::PARAM_INT);
130
        $stmt->bindValue('total_dw_dns_zones', $total_dw_dns_zones, \PDO::PARAM_INT);
131
        $stmt->bindValue('total_dw_records', $total_dw_records, \PDO::PARAM_INT);
132
        $bind_timestamp = $this->time->stamp();
133
        $stmt->bindValue('insert_time', $bind_timestamp, \PDO::PARAM_STR);
134
        $stmt->execute();
135
136
    }
137
138
    public function getServerTotals()
139
    {
140
        $result = $this->system->db()->query("
141
            SELECT dw_accounts, dw_dns_zones, dw_dns_records
142
            FROM dw_server_totals")->fetch();
143
144
        return array($result->dw_accounts, $result->dw_dns_zones, $result->dw_dns_records);
145
    }
146
147
    public function checkForServerTotalsTable()
148
    {
149
        return $this->system->db()->query("SHOW TABLES LIKE 'dw_server_totals'")->fetchColumn();
150
    }
151
152
} //@formatter:on
153