Passed
Push — master ( 706253...6eb40c )
by Jan
14:35
created

DBInfoHelper::getDatabaseSize()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 0
dl 0
loc 19
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 *  Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 *  This program is free software: you can redistribute it and/or modify
8
 *  it under the terms of the GNU Affero General Public License as published
9
 *  by the Free Software Foundation, either version 3 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU Affero General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU Affero General Public License
18
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\Services\Misc;
22
23
use Doctrine\DBAL\Connection;
24
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
25
use Doctrine\DBAL\Platforms\SqlitePlatform;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Platforms\SqlitePlatform was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Doctrine\ORM\EntityManagerInterface;
27
28
/**
29
 * This service provides db independent information about the database.
30
 */
31
class DBInfoHelper
32
{
33
    protected Connection $connection;
34
    protected EntityManagerInterface $entityManager;
35
36
    public function __construct(EntityManagerInterface $entityManager)
37
    {
38
        $this->entityManager = $entityManager;
39
        $this->connection = $entityManager->getConnection();
40
    }
41
42
    /**
43
     * Returns the database type of the used database.
44
     * @return string|null Returns 'mysql' for MySQL/MariaDB and 'sqlite' for SQLite. Returns null if unknown type
45
     */
46
    public function getDatabaseType(): ?string
47
    {
48
        if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
49
            return 'mysql';
50
        }
51
52
        if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
53
            return 'sqlite';
54
        }
55
56
        return null;
57
    }
58
59
    /**
60
     * Returns the database version of the used database.
61
     * @return string|null
62
     * @throws \Doctrine\DBAL\Exception
63
     */
64
    public function getDatabaseVersion(): ?string
65
    {
66
        if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
67
            return $this->connection->fetchOne('SELECT VERSION()');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->connection...One('SELECT VERSION()') could return the type false which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
68
        }
69
70
        if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
71
            return $this->connection->fetchOne('SELECT sqlite_version()');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->connection...LECT sqlite_version()') could return the type false which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
72
        }
73
74
        return null;
75
    }
76
77
    /**
78
     * Returns the database size in bytes.
79
     * @return int|null The database size in bytes or null if unknown
80
     * @throws \Doctrine\DBAL\Exception
81
     */
82
    public function getDatabaseSize(): ?int
83
    {
84
        if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
85
            try {
86
                return $this->connection->fetchOne('SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = DATABASE()');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->connection...e_schema = DATABASE()') could return the type false which is incompatible with the type-hinted return integer|null. Consider adding an additional type-check to rule them out.
Loading history...
87
            } catch (\Doctrine\DBAL\Exception $e) {
88
                return null;
89
            }
90
        }
91
92
        if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
93
            try {
94
                return $this->connection->fetchOne('SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->connection..., pragma_page_size();') could return the type false which is incompatible with the type-hinted return integer|null. Consider adding an additional type-check to rule them out.
Loading history...
95
            } catch (\Doctrine\DBAL\Exception $e) {
96
                return null;
97
            }
98
        }
99
100
        return null;
101
    }
102
103
104
}