Typo3DatabaseBackend::countEntries()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Stats service for TYPO3 database
4
 *
5
 * @package CacheCheck\Service\Statistics
6
 * @author  Tim Lochmüller
7
 */
8
9
namespace HDNET\CacheCheck\Service\Statistics;
10
11
use HDNET\CacheCheck\Domain\Model\Cache;
12
use TYPO3\CMS\Core\Database\DatabaseConnection;
13
14
/**
15
 * Stats service for TYPO3 database
16
 *
17
 * @author Tim Lochmüller
18
 */
19
class Typo3DatabaseBackend implements StatisticsInterface
20
{
21
22
    /**
23
     * Get the number of entries
24
     *
25
     * @param Cache $cache
26
     *
27
     * @return int
28
     */
29
    public function countEntries(Cache $cache)
30
    {
31
        return (int)$this->getDatabaseConnection()
32
            ->exec_SELECTcountRows('*', 'cf_' . $cache->getName(), '1=1');
33
    }
34
35
    /**
36
     * Get the size of the cache in byte
37
     *
38
     * @param Cache $cache
39
     *
40
     * @return float
41
     */
42
    public function getSize(Cache $cache)
43
    {
44
        if ($this->countEntries($cache) === 0) {
45
            return 0.0;
46
        }
47
48
        $databaseConnection = $this->getDatabaseConnection();
49
        $query = 'SELECT data_length FROM information_schema.TABLES WHERE table_schema="' . $GLOBALS['TYPO3_CONF_VARS']['DB']['database'] . '" AND table_name="cf_' . $cache->getName() . '"';
50
        $res = $databaseConnection->sql_query($query);
51
        $info = $databaseConnection->sql_fetch_assoc($res);
52
        return (float)$info['data_length'];
53
    }
54
55
    /**
56
     * Get the number of tags
57
     *
58
     * @param Cache $cache
59
     *
60
     * @return int
61
     */
62
    public function countTags(Cache $cache)
63
    {
64
        return (int)$this->getDatabaseConnection()
65
            ->exec_SELECTcountRows('DISTINCT tag', 'cf_' . $cache->getName() . '_tags', '1=1');
66
    }
67
68
    /**
69
     * Get database connection
70
     *
71
     * @return DatabaseConnection
72
     */
73
    protected function getDatabaseConnection()
74
    {
75
        return $GLOBALS['TYPO3_DB'];
76
    }
77
78
    /**
79
     * Returns the age of found entries in seconds
80
     *
81
     * @param Cache $cache
82
     *
83
     * @return null
84
     */
85
    public function getAge(Cache $cache)
86
    {
87
        return null;
88
    }
89
90
    /**
91
     * Returns the age of found entries in seconds// DOES NOT. RETURNS LEFT LIFETIME
92
     *
93
     * @param Cache $cache
94
     *
95
     * @return int|null
96
     */
97
    public function getExpires(Cache $cache)
98
    {
99
        if ($this->countEntries($cache) === 0) {
100
            return null;
101
        }
102
        $databaseConnection = $this->getDatabaseConnection();
103
        $query = "SELECT AVG(CAST(expires AS SIGNED INTEGER) - UNIX_TIMESTAMP()) as life_time_left FROM cf_" . $cache->getName() . " WHERE expires > UNIX_TIMESTAMP() AND expires < 2145909600";
104
        $res = $databaseConnection->sql_query($query);
105
        $info = $databaseConnection->sql_fetch_assoc($res);
106
        return (int)$info['life_time_left'];
107
    }
108
}
109